我想为我的ASP.NET核心应用程序设置HTTPS,以及定义我的应用程序可以监听的不同端口(例如,有点像Scot Hanselman前面所展示的那样:https://channel9.msdn.com/Events/Build/2017/B8048)
我查了官方资源:
我认为默认构建器应该在appsettings.json(或相关的appsettings.Development.json)中挖掘。
Program.cs 内容:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
var builder = WebHost.CreateDefaultBuilder(args);
return builder.UseStartup<Startup>().Build();
}
}
appsettings.Development.json 内容:
{
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"Console": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
},
"Kestrel": {
"Endpoints": {
"Localhost": {
"Address": "127.0.0.1",
"Port": "5001"
}
}
},
"LocalhostwithHttps": {
"Address": "127.0.0.1",
"Port": "44333",
"Certificate": {
"Source": "Store",
"StoreLocation": "LocalMachine",
"StoreName": "My",
"Subject": "CN=localhost"
}
}
}
唉,当开始针对开发环境进行调试时,我的Web应用程序最终会以这样的方式开始:
Hosting environment: Development
Content root path: c:\Users\ehoua\Desktop\There
Launching browser (cmd.exe /C start http://localhost:5000)
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
我是否必须真正放弃默认构建器并指定应用程序需要依赖的appsettings或硬编码端口值...?
[编辑]
在@Set回答之后,我在Program.cs
中尝试了以下内容:
public static IWebHost BuildWebHost(string[] args)
{
var developmentConfiguration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile($"appsettings.Development.json", optional: false)
.Build();
var productionConfiguration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile($"appsettings.json", optional: false)
.Build();
return WebHost.CreateDefaultBuilder(args)
.UseEnvironment("Development").UseConfiguration(developmentConfiguration)
.UseEnvironment("Production").UseConfiguration(productionConfiguration)
.UseStartup<Startup>().Build();
}
我故意将可选参数设置为false,以便仔细检查appsettings文件是否已正确添加到配置中。还检查了Startup Configure
方法中的环境是开发还是生产,但最终仍然使用了属于我的appsettings文件的旧端口5000。
[编辑] 在.NET 2.1中可能
请参阅:https://github.com/aspnet/KestrelHttpServer/issues/1290#issuecomment-353416087
答案 0 :(得分:3)
您似乎需要直接致电UseConfiguration
以设置从appsettings.json
读取的网址:
public static IWebHost BuildWebHost(string[] args)
{
var config = new ConfigurationBuilder()
.AddJsonFile($"appsettings.Development.json", optional: true)
.Build();
return WebHost.CreateDefaultBuilder(args)
.UseConfiguration(config)
.UseStartup<Startup>()
.Build();
}
CreateDefaultBuilder
方法实现在内部使用ConfigureAppConfiguration
方法,而不是UseConfiguration
。并且存在差异(来自相关的Github issue):
ConfigureAppConfiguration
旨在配置应用服务中的IConfiguration
,而UseConfiguration
旨在配置WebHostBuilder
的设置。由于网址是WebHostBuilder
上的设置,因此只有UseConfiguration
才能使用appsetting.json
。请注意,WebHostBuilder的配置已添加到应用程序的配置中,但相反的情况并非如此;配置应用程序的配置不会影响WebHostBuilder的配置。
&#34; Kestel.Endpoints&#34;您的{
...
"urls": "http://127.0.0.1:5001"
}
部分应该只包含以下内容:
<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
<item name="android:buttonStyle">@style/MyButton</item>
</style>