UseIISIntegration will set PreferHostingUrls after we address https://github.com/aspnet/IISIntegration/issues/352
Determining Hosting Environment While Configuring Kestrel and UseHttps
In the ASP.NET Core Main method below, how can I determine the hosting environment, so I can switch between different certificate files for HTTPS?
public sealed class Program
{
public static void Main(string[] args)
{
new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseKestrel(
options =>
{
if ([Development Hosting Environment])
{
options.UseHttps("DevelopmentCertificate.pfx");
}
else
{
options.UseHttps("ProductionCertificate.pfx");
}
})
.UseIISIntegration()
.UseStartup<startup>()
.Build()
.Run();
}
}
</startup>
该提问来源于开源项目：aspnet/KestrelHttpServer
 点赞
 写回答
 关注问题
 收藏
 复制链接分享
 邀请回答
20条回答

采纳
点赞 评论 复制链接分享

采纳
ahh thank you didn't know there was PreferHostingUrls thing any documentation on that?
点赞 评论 复制链接分享 
采纳
it's new for 2.0: https://github.com/aspnet/Hosting/commit/4cdc970055ef2ad77887109c4acde89278e0ce18
点赞 评论 复制链接分享 
采纳
This covers https://github.com/aspnet/KestrelHttpServer/issues/1713 too.
点赞 评论 复制链接分享 
采纳
Is is possible to set UseHttps from Startup class?
点赞 评论 复制链接分享 
采纳
Yes:
services.Configure<kestrelserveroptions>(options => { options.UseHttps(...); }); </kestrelserveroptions>
点赞 评论 复制链接分享 
采纳
I was able to find a slightly shorter way before this gets implemented properly:
public static IHostingEnvironment hostingEnvironment; public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<startup>() .ConfigureAppConfiguration((hostingContext, config) => { hostingEnvironment = hostingContext.HostingEnvironment; }) .UseKestrel(options => { if (hostingEnvironment.IsDevelopment()) { options.Listen(IPAddress.Loopback, 443, listenOptions => { listenOptions.UseHttps("certificate.pfx", "password"); }); } }) .Build(); </startup>
点赞 评论 复制链接分享 
采纳
Another way to do this:
public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<startup>() .UseKestrel() .ConfigureServices((context, services) => { services.Configure<kestrelserveroptions>(options => { if (context.HostingEnvironment.IsDevelopment()) { options.Listen(IPAddress.Loopback, 443, listenOptions => { listenOptions.UseHttps("certificate.pfx", "password"); }); } }); }) .Build(); </kestrelserveroptions></startup>
点赞 评论 复制链接分享 
采纳
It was just answered on another thread that I just saw: https://github.com/aspnet/Home/issues/1915#issuecomment276133606
... and if you don't use commandline config, env var config, a config file, etc., then you can read the
ASPNETCORE_ENVIRONMENT
env var directly after you manually set it somehow.点赞 评论 复制链接分享 
采纳
It turns out you can use
ConfigureServices
to get hold ofIHostingEnvironment
like so:public sealed class Program { public static void Main(string[] args) { IHostingEnvironment hostingEnvironment = null; new WebHostBuilder() .UseContentRoot(Directory.GetCurrentDirectory()) .ConfigureServices( services => { hostingEnvironment = services .Where(x => x.ServiceType == typeof(IHostingEnvironment)) .Select(x => (IHostingEnvironment)x.ImplementationInstance) .First(); }) .UseKestrel( options => { if (hostingEnvironment.IsDevelopment()) { // Use a selfsigned certificate to enable 'dotnet run' to work in development. options.UseHttps("DevelopmentCertificate.pfx", "password"); } }) .UseIISIntegration() .UseStartup<startup>() .Build() .Run(); } } </startup>
It really feels like this should be easier.
点赞 评论 复制链接分享 
采纳
Assigning to for investigation
点赞 评论 复制链接分享 
采纳
How can you handle the .UseUrls() to wrap an if statment around it?
// Only add if development?? .UseUrls("https://localhost:5001")
点赞 评论 复制链接分享 
采纳
Couldn't you do something like:
.UseUrls(hostingEnvironment.IsDevelopment() ? "http://*:8080" : "https://*:8080")
You can get
hostingEnvironment
like I showed but this should really be easier.点赞 评论 复制链接分享 
采纳
that's pretty cool, but what happens if you publish to an IIS environment?
点赞 评论 复制链接分享 
采纳
Well, that would be a Production environment, so in my example above it would use the https URL.
点赞 评论 复制链接分享 
采纳
thanks for that, I wasn't sure if IIS would still be able to use the .UseUrls() for a live site like foo.com
.UseUrls(hostingEnvironment.IsDevelopment() ? "http://*:8080" : "https://*:443")
点赞 评论 复制链接分享 
采纳
IIS overwrites UseUrls.
点赞 评论 复制链接分享 
采纳
ahh thank you I didn't know that. What about options.UseHttps() does that get overridden?
点赞 评论 复制链接分享 
采纳
It will (though that may be broken right now in dev). Do you have UseIISIntegration setting the new PreferHostingUrls?
点赞 评论 复制链接分享 
采纳
has some ideas for another (nicer) workaround.
点赞 评论 复制链接分享
相关推荐
 回答 2 已采纳 Description A cyclic number is an integer n digits in length which, when multiplied by any integer from 1 to n, yields a"cycle"of the digits of the original number. That is, if you consider the number after the last digit to "wrap around"back to the first digit, the sequence of digits in both numbers will be the same, though they may start at different positions.For example, the number 142857 is cyclic, as illustrated by the following table: 142857 *1 = 142857 142857 *2 = 285714 142857 *3 = 428571 142857 *4 = 571428 142857 *5 = 714285 142857 *6 = 857142 Input Write a program which will determine whether or not numbers are cyclic. The input file is a list of integers from 2 to 60 digits in length. (Note that preceding zeros should not be removed, they are considered part of the number and count in determining n. Thus, "01"is a twodigit number, distinct from "1" which is a onedigit number.) Output For each input integer, write a line in the output indicating whether or not it is cyclic. Sample Input 142857 142856 142858 01 0588235294117647 Sample Output 142857 is cyclic 142856 is not cyclic 142858 is not cyclic 01 is not cyclic 0588235294117647 is cyclic
 4年前回答 1 已采纳 Time.deltatime怎么理解， 是不是 float speed = 10f; void Update(){ transform.Translate(0, speed, 0); transform.Translate(0, speed * Time.deltatime, 0); } 假如帧率为10 deltatime就是0.1秒每帧 前者的移动是10米每帧 后者的移动是10米 * 0.1秒 * / (帧的平方) 是吗
 回答 2 已采纳 The Transferable Vote system for determining the winner of an election requires that a successful candidate obtain an absolute majority of the votes cast, even when there are more than two candidates. To achieve this goal, each voter completes his or her ballot by listing all the candidates in order of preference. Thus if there are five candidates for a single position, each voter's ballot must contain five choices, from first choice to fifth choice. In this problem you are to determine the winner, if any, of elections using the Transferable Vote system. If there are c candidates for the single position, then each voter's ballot will include c distinct choices, corresponding to identifying the voter's first place, second place, , and nth place selections. Invalid ballots will be discarded, but their presence will be noted. A ballot is invalid if a voter's choices are not distinct (choosing the same candidate as first and second choice isn't permitted) or if any of the choices aren't legal (that is, in the range 1 to c). For each election candidates will be assigned sequential numbers starting with 1. The maximum number of voters in this problem will be 100, and the maximum number of candidates will be 5. Table A Table B   Voter First Second Third Choice Choice Choice 1 1 2 4 2 1 3 2 1 3 2 3 3 2 1 3 2 1 4 3 2 1 3 2 1 5 1 2 3 1 2 3 6 2 3 1 3 1 7 3 2 1 3 2 1 8 3 1 1 9 3 2 1 3 2 1 10 1 2 3 1 2 3 11 1 3 2 1 3 2 12 2 3 1 3 1 Consider a very small election. In Table A are the votes from the 12 voters for the three candidates. Voters 1 and 8 cast invalid ballots; they will not be counted. This leaves 10 valid ballots, so a winning candidate will require at least 6 votes (the least integer value greater than half the number of valid ballots) to win. Candidates 1 and 3 each have 4 first choice votes, and candidate 2 has two. There is no majority, so the candidate with the fewest first choice votes, candidate 2, is eliminated. If there had been several candidates with the fewest first choice votes, any of them, selected at random, could be selected for elimination. With candidate 2 eliminated, we advance the second and third choice candidates from those voters who voted for candidate 2 as their first choice. The result of this action is shown in Table B. Now candidate 3 has picked up 2 additional votes, giving a total of 6. This is sufficient for election. Note that if voter 12 had cast the ballot ``2 1 3" then there would have been a tie between candidates 1 and 3. Input There will be one or more elections to consider. Each will begin with a line containing the number of candidates and the number of voters, c and n. Data for the last election will be followed by a line containing two zeroes. Following the first line for each election will be n additional lines each containing the choices from a single ballot. Each line will contain only c nonnegative integers separated by whitespace. Output For each election, print a line identifying the election number (they are numbered sequentially starting with 1). If there were any invalid ballots, print an additional line specifying the number of such. Finally, print a line indicating the winner of the election, if any, or indication of a tie; be certain to identify the candidates who are tied. Look at the samples below for the exact output format. Sample Input 3 12 1 2 4 1 3 2 3 2 1 3 2 1 1 2 3 2 3 1 3 2 1 3 1 1 3 2 1 1 2 3 1 3 2 2 3 1 3 12 1 2 4 1 3 2 3 2 1 3 2 1 1 2 3 2 3 1 3 2 1 3 1 1 3 2 1 1 2 3 1 3 2 2 1 3 4 15 4 3 1 2 4 1 2 3 3 1 4 2 1 3 2 4 4 1 2 3 3 4 2 1 2 4 3 1 3 2 1 4 3 1 4 2 1 4 2 3 3 4 1 2 3 2 1 4 4 1 3 2 3 2 1 4 4 2 1 4 0 0 Sample Output Election #1 2 bad ballot(s) Candidate 3 is elected. Election #2 2 bad ballot(s) The following candidates are tied: 1 3 Election #3 1 bad ballot(s) Candidate 3 is elected.
 回答 3 已采纳 Description Graph algorithms form a very important part of computer science and have a lineage that goes back at least to Euler and the famous Seven Bridges of Konigsberg problem. Many optimization problems involve determining efficient methods for reasoning about graphs. This problem involves determining a route for a postal worker so that all mail is delivered while the postal worker walks a minimal distance, so as to rest weary legs. Given a sequence of streets (connecting given intersections) you are to write a program that determines the minimal cost tour that traverses every street at least once. The tour must begin and end at the same intersection. The ``reallife'' analogy concerns a postal worker who parks a truck at an intersection and then walks all streets on the postal delivery route (delivering mail) and returns to the truck to continue with the next route. The cost of traversing a street is a function of the length of the street (there is a cost associated with delivering mail to houses and with walking even if no delivery occurs). In this problem the number of streets that meet at a given intersection is called the degree of the intersection. There will be at most two intersections with odd degree. All other intersections will have even degree, i.e., an even number of streets meeting at that intersection. Input The input consists of a sequence of one or more postal routes. A route is composed of a sequence of street names (strings), one per line, and is terminated by the string ``deadend'' which is NOT part of the route. The first and last letters of each street name specify the two intersections for that street, the length of the street name indicates the cost of traversing the street. All street names will consist of lowercase alphabetic characters. For example, the name foo indicates a street with intersections f and o of length 3, and the name computer indicates a street with intersections c and r of length 8. No street name will have the same first and last letter and there will be at most one street directly connecting any two intersections. As specified, the number of intersections with odd degree in a postal route will be at most two. In each postal route there will be a path between all intersections, i.e., the intersections are connected. Output For each postal route the output should consist of the cost of the minimal tour that visits all streets at least once. The minimal tour costs should be output in the order corresponding to the input postal routes. Sample Input one two three deadend mit dartmouth linkoping tasmania york emory cornell duke kaunas hildesheim concord arkansas williams glasgow deadend Sample Output 11 114
 回答 1 已采纳 Description Phil Kropotnik is a game maker, and one common problem he runs into is determining the set of dice to use in a game. In many current games, nontraditional dice are often required, that is, dice with more or fewer sides than the traditional 6sided cube. Typically, Phil will pick random values for all but the last die, then try to determine specific values to put on the last die so that certain sums can be rolled with certain probabilities (actually, instead of dealing with probabilities, Phil just deals with the total number of different ways a given sum can be obtained by rolling all the dice). Currently he makes this determination by hand, but needless to say he would love to see this process automated. That is your task. For example, suppose Phil starts with a 4sided die with face values 1, 10, 15, and 20 and he wishes to determine how to label a 5sided die so that there are a) 3 ways to obtain a sum of 2, b) 1 way to obtain a sum of 3, c) 3 ways to obtain 11, d) 4 ways to obtain 16, and e)1 way to obtain 26. To get these results he should label the faces of his 5sided die with the values 1, 1, 1, 2, and 6. (For instance, the sum 16 may be obtained as 10 +6 or as 15 +1, with three different "1" faces to choose from on the second die, for a total of 4 different ways.) Input Input will consist of multiple input sets. Each input set will start with a single line containing an integer n indicating the number of dice that are already specified. Each of the next n lines describes one of these dice. Each of these lines will start with an integer f (indicating the number of faces on the die) followed by f integers indicating the value of each face. The last line of each problem instance will have the form r m v1 c1 v2 c2 v3 c3 ... vm cm where r is the number of faces required on the unspecified die, m is the number of sums of interest, v1, ... ,vm are these sums, and c1, ... ,cm are the counts of the desired number of different ways in which to achieve each of the respective sums. Input values will satisfy the following constraints: 1 <= n <= 20, 3 <= f <= 20, 1 <= m <= 10, and 4 <= r <= 6. Values on the faces of all dice, both the specified ones and the unknown die, will be integers in the range 1 ... 50, and values for the vi's and ci’s are all nonnegative and are strictly less than the maximum value of a 32bit signed integer. The last input set is followed by a line containing a single 0; it should not be processed. Output For each input set, output a single line containing either the phrase "Final die face values are" followed by the r face values in nondescending order, or the phrase "Impossible" if no die can be found meeting the specifications of the problem. If there are multiple dice which will solve the problem, choose the one whose lowest face value is the smallest; if there is still a tie, choose the one whose secondlowest face value is smallest, etc. Sample Input 1 4 1 10 15 20 5 5 2 3 3 1 11 3 16 4 26 1 1 6 1 2 3 4 5 6 6 3 7 6 2 1 13 1 4 6 1 2 3 4 5 6 4 1 2 2 3 3 3 7 9 8 1 4 5 9 23 24 30 38 4 4 48 57 51 37 56 31 63 11 0 Sample Output Final die face values are 1 1 1 2 6 Impossible Final die face values are 3 7 9 9
 回答 3 已采纳 详细描述见博文，不知道为什么，在这里上传图片看不了 https://blog.csdn.net/Ambitious_DJK/article/details/83855796
 4年前回答 2 已采纳 Introduction Boudreaux and Thibodaux have just seen the Matrix, and are arguing whether or not it would actually be possible to dodge bullets if you moved fast enough. Boudreaux is used to hunting nutria with buckshot, and maintains that most bullet spreads should make it impossible to dodge all the bullets. Thibodaux is still doing kungfu in his head, and is sure that if you could move fast enough it would be possible to dodge the bullets. Boudreaux is getting fed up, and is about ready to prove his point with some empirical testing. Your job is to come up with a way to prove or disprove this hypothesis for different scenarios using computer simulations, which are much safer than the methods that Boudreaux has in mind. Input Input to this problem will consist of a (nonempty) series of up to 20 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets. A single data set has 5 components: Start line  A single line, "START N", where N is a positive integer in the range 1 , , with no spaces. Velocities may be negative values. Output Find out if the target got out 'da way. A hit occurs when any bullet passes through a grid square with mass in it. For the sake of simplicity, the target can be assumed to be two dimensional, and the bullets volumeless. Also, Boudreaux and Thibideaux don't even know what precision means, they just make sure to calculate to at least four decimal places. (In other words, don't worry about precision edge detection. Just keep the math simple and use very precise numbers to minimize error. You'll find hits in the correct places.) For each data set, there will be exactly one output set, and there will be exactly one blank line separating output sets. An output set will take one of two forms: If no bullets hit masscontaining parts of the target, your output will be, "Got Out Da Way!" Otherwise (if at least one hit occurred), your output will be the Target, with all hit blocks replaced with asterisks ('*'). Sample Input START 3 10,2,1 300,14.5,20 350,80,0 400,28.75,26 @@@@ n #^^# oU ## o ooooooooooo o oooooo o oooo o DDDD oo DDDD D D D D TT TT TT TT END START 2 10,2,1 300,14.5,20 350,80,0 @@@@ n #^^# oU ## o ooooooooooo o oooooo o oooo o DDDD oo DDDD D D D D TT TT TT TT END Sample Output @@@@ * #^^# oU ## o ooooooooooo o oooooo o oooo o DDDD oo DDDD D D D D TT TT TT TT Got Out Da Way!
 互联网老辛的博客 所用系统为centos6.6系统，yum源为网络源 Centons下出现这个问题 其大概意思是fastestmirror不能使用，fastestmirror是yum的一个加速插件，系统不支持或者缺少组建导致的。处理办法就是禁用这个插件，方法如下...
 YZC1892的博客 最简单的有效的方法，重新打开安装软件就行了！
 小星向上的博客 解决方案1: 我的电脑 > 启动任务管理器 > 找到VisualSVN Server,右键 "Stop" 停掉该服务；然后重新安装，问题解决！ 解决方案2: ... 下装了360的话，也可能出现此问题,把360有关开启的服务全部关闭,重新安装就...
 gxchaha的博客 TX2刷机安装jetpack时，flashOS finished后，卡在determining IP adress。 解决方案： 点击安装界面的cancel按键退出，重新执行JetPack****.run文件，选择安装组件时将target 的flashOS设定为no action,其他都选上...
 seamanj的博客 Managing environments Creating an environment with ...Creating an environment from an environment.yml file Specifying a location for an environment Updating an environment Cloning an environment...
 wolf96的博客 The Electromagnetic and Visible Spectra 原文：http://www.physicsclassroom.com/class/light/Lesson2/TheElectromagneticandVisibleSpectra The Electromagnetic and Visible SpectraVisible Light a
 Jay_1989的博客 2、于是采用ifup eth0命令来启用网络接口eth0，发现无法启动，报"Determining IP information for eth0... failed; no link present. Check cable?" 解决方案： 1、虚拟机与本机采用的连接方式为：Host...
 CentOS之——Bringing up interface eth0: Determining if ip address 192.168.1.81 is already in use for de4年前冰 河的博客 重启网卡出现提示： Bringing up interface eth0: Determining if ip address 192.168.1.81 is already in use for device eth0... [ OK ] Bringing up inte
 走码观花的博客 最近用vmvare搭建一台虚拟机，本来没什么问题，但是每次重启启动...2.servce network start命令后，报错这个：Determining IP information for eth0... failed. 看了网上很多文章，说改BOOTPROTO=none，感觉都
 G_rrrr的博客 设定周期性的闹铃提醒和后台服务，最常见的用途是定期更新应用程序的数据，从互联网上下载资源，缓存数据或者执行长时间的下载任务。但是如果设备当前没有连接到网络，或者是网络状况不稳定，连接太慢，不能正常完成...
 2年前DrogoZhang的博客 Poster & Demo Session Social Media and Computational Social Science, Text Mining and NLP ...Integrating Text and Image: Determining Multimodal Document Intent in Instagram Posts (#52) [...
 倚楼听风雨，淡看江湖路的博客 fastestmirror是yum的一个加速插件，报错是系统不支持或者缺少组建导致的。所以，禁用这个插件 vi /etc/yum/pluginconf.d/fastestmirror.conf [main] enabled=0 //把1改为0 ...hostfilepath=/var/cache/yum/...
 DrogoZhang的博客 Poster &...Sentiment Analysis and Argument Mining, Lexical Semantics, Sentencelevel Semantics MultiRelational Word Embeddings for Selectional Preferences (#623) MulCode: A Multiplica...