Chr*_*ris 5 c# asp.net-core asp.net-core-2.2
我写了一个 ASP.Net Core 2.2 应用程序。当我在我的开发机器上运行它时,一切正常。
我已将它作为一个独立的应用程序发布并部署到我的登台机器上。
TargetFramework 是 netcoreapp2.2。RuntimeIdentifier 是 win-x64。环境正在上演。
当通过命令行运行应用程序进行一些测试时,它似乎没有读取 appsettings.staging.json 或任何 appsettings.json 文件。
出于测试目的,我将 Startup.cs 的配置方法设置如下:
public void Configure( IApplicationBuilder app , IHostingEnvironment env )
{
if( env.IsDevelopment( ) )
{
app.UseDeveloperExceptionPage( );
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts( );
}
app.UseHttpsRedirection( );
Console.WriteLine( $"Chris Environment: {env.EnvironmentName}" );
Console.WriteLine( $"Chris IsStaging: {env.IsStaging( )}" );
Console.WriteLine( $"Chris ConnectionString: {Configuration.GetConnectionString( "DefaultConnection" )}" );
Console.WriteLine( $"Chris LoggingAPI: {Configuration["LoggingAPIURL"]}" );
foreach( var test in Configuration.AsEnumerable( ) )
{
var key = test.Key;
var val = test.Value;
Console.WriteLine( $"Chris Key: {key} - Value: {val}" );
}
app.UseMvc( b =>
{
b.Select( ).Expand( ).Filter( ).OrderBy( ).MaxTop( 100 ).Count( );
b.MapODataServiceRoute( "default" , "api" , EdmModelBuilder.GetEdmModel( app.ApplicationServices ) );
} );
}
Run Code Online (Sandbox Code Playgroud)
我通过在命令行中键入来运行该应用程序:path/To/My/App.exe --environment Staging
写出的结果是: Chris Environment: Staging Chirs IsStaging: True Chris ConnectionString: Chris LoggingAPI:
连接字符串和 LoggingAPI 留空。循环返回一堆值,但没有任何 appsettings.json 文件中的任何内容。
我的 appsettings.json 文件如下所示:
{
"ConnectionStrings": {
"DefaultConnection": "Some ConnectionString"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"LoggingAPIURL": "SomeURL"
}
Run Code Online (Sandbox Code Playgroud)
我已经验证 appsetting.json 文件在服务器上。
有人可以向我解释发生了什么吗?
WebHostBuilder配置的基本路径设置为IHostingEnvironment.ContentRootPath(源):
var builder = new ConfigurationBuilder()
.SetBasePath(_hostingEnvironment.ContentRootPath)
Run Code Online (Sandbox Code Playgroud)
当 usingWebHostBuilder.CreateDefaultBuilder是项目模板生成的默认方法时,IHostingEvironment.ContentRootPath使用Directory.GetCurrentDirectory()(source)设置:
builder.UseContentRoot(Directory.GetCurrentDirectory());
Run Code Online (Sandbox Code Playgroud)
这意味着,当尝试定位appsettings.json和 时appsettings.[Environment].json,将使用工作目录,而不一定是应用程序的目录。在您的示例中,您从其自己的目录之外运行应用程序,这意味着.json找不到文件。
要解决此问题,您可以先cd进入path/To/My然后运行App.exe从那里。或者,如果您要App.exe作为服务运行,则可以将工作目录设置为与应用程序本身相同的目录。
| 归档时间: |
|
| 查看次数: |
2260 次 |
| 最近记录: |