如何让 .Net Core 3.1 查看我的应用程序设置文件
How to get .Net Core 3.1 to see my appsettings files
我使用 .net core 3.1 创建了一个 api,就像这些东西一样,它可以在我的机器上运行。但是当我尝试将它发布到 IIS 时,它在我的机器上也不起作用。我必须使用 IIS 作为我的要求之一。所以错误是:
- HTTP 错误 500.30 - ANCM 进程中启动失败。我尝试了很多东西,但似乎没有任何效果。我查看了 appsettings 文件以查看是否遗漏了逗号或其他内容,并更改了 IIS 中的设置,以便应用程序池不管理代码并禁用 32 位应用程序选项。
- 未处理的异常。 System.IO.FileNotFoundException: 找不到文件 'C:\inetpub\wwwroot\appnamehere\appsettings..json'。我在事件查看器中找到了这个。
事件查看器错误如下:
Application '/LM/W3SVC/2/ROOT' with physical root 'C:\inetpub\wwwroot\appnamehere\' failed to load coreclr. Exception message:
CLR worker thread exited prematurely
Application '/LM/W3SVC/2/ROOT' with physical root 'C:\inetpub\wwwroot\appnamehere\' hit unexpected managed exception, exception code = '0xe0434352'. First 30KB characters of captured stdout and stderr logs:
Unhandled exception. System.IO.FileNotFoundException: Could not find file 'C:\inetpub\wwwroot\appnamehere\appsettings..json'.
File name: 'C:\inetpub\wwwroot\appnamehere\appsettings..json'
at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamReader.ValidateArgsAndOpenPath(String path, Encoding encoding, Int32 bufferSize)
at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks)
at System.IO.File.InternalReadAllText(String path, Encoding encoding)
at System.IO.File.ReadAllText(String path)
at appnamehere.Services.Configurations.DatabaseConfigProvider.FetchConfiguration() in C:\Development\solutionNameHere\appnamehere.Services.Configurations\DatabaseConfigProvider.cs:line 25
at appnamehere.Services.Configurations.DatabaseConfigProvider.Load() in C:\Development\solutionNameHere\appnamehere.Services.Configurations\DatabaseConfigProvider.cs:line 20
at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList1 providers)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
at appnamehere.Services.Configurations.AppConfiguration.GetCurrentSettings() in C:\Development\solutionNameHere\appnamehere.Services.Configurations\AppConfiguration.cs:line 34
at appnamehere.Services.Configurations.AppConfiguration.get_Current() in C:\Development\solutionNameHere\appnamehere.Services.Configurations\AppConfiguration.cs:line 24
at appnamehere.Services.Data.Implementation.TitanDbContext.OnConfiguring(DbContextOptionsBuilder optionsBuilder) in C:\Development\solutionNameHere\appnamehere.Services.Data.Implementation\TitanDbContext.cs:line 17
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
at Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure
1 accessor)
at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.get_DatabaseCreator()
at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated()
at appnamehere.Startup..ctor(IConfiguration configuration) in C:\Development\solutionNameHere\appnamehere\Startup.cs:line 27
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services)
at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass12_0.b__0(HostBuilderContext context, IServiceCollection services)
at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
at Microsoft.Extensions.Hosting.HostBuilder.Build()
at appnamehere.Program.Main(String[] args) in C:\Development\solutionNameHere\appnamehere\Program.cs:line 20
所以我去了那个位置,那里我所有的应用程序设置文件都是 appsettings.json 和 appsettings.Development.json。
然后我去看看哪里出错了。
public class Program
{
public static void Main(string[] args)
{
try
{
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var host = CreateHostBuilder(args, env).Build();
host.Run();
}
catch (Exception ex)
{
throw;
}
finally
{
NLog.LogManager.Shutdown();
}
}
public static IHostBuilder CreateHostBuilder(string[] args, string env) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.ConfigureAppConfiguration((IConfigurationBuilder builder) =>
{
builder.Sources.Clear();
builder.SetBasePath(Directory.GetCurrentDirectory());
builder.AddDataBaseProvider();
builder.AddJsonFile($"appsettings.{env}.json");
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.ClearProviders();
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
logging.AddEventSourceLogger();
logging.AddFile(hostingContext.Configuration.GetSection("Logging"));
});
}
我尝试硬编码 builder.AddJsonFile($"appsettings.{env}.json");
以明确引用 appsettings.Development.json,但错误仍然存在。
如何让应用程序在我发布后看到 appsettings.Development.json 文件?当我 运行 通过 Visual Studio.
时,它工作得很好
我发现了问题。该站点的应用程序池使用默认的 ApplicationPoolIdentity 而不是我的本地用户。所以它没有读取用户环境变量,因此它在 Visual Studio 中工作但不是通过 IIS。
我的解决方案是更改
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
至
var env = "name-Of-File"
我使用 .net core 3.1 创建了一个 api,就像这些东西一样,它可以在我的机器上运行。但是当我尝试将它发布到 IIS 时,它在我的机器上也不起作用。我必须使用 IIS 作为我的要求之一。所以错误是:
- HTTP 错误 500.30 - ANCM 进程中启动失败。我尝试了很多东西,但似乎没有任何效果。我查看了 appsettings 文件以查看是否遗漏了逗号或其他内容,并更改了 IIS 中的设置,以便应用程序池不管理代码并禁用 32 位应用程序选项。
- 未处理的异常。 System.IO.FileNotFoundException: 找不到文件 'C:\inetpub\wwwroot\appnamehere\appsettings..json'。我在事件查看器中找到了这个。
事件查看器错误如下:
Application '/LM/W3SVC/2/ROOT' with physical root 'C:\inetpub\wwwroot\appnamehere\' failed to load coreclr. Exception message: CLR worker thread exited prematurely
Application '/LM/W3SVC/2/ROOT' with physical root 'C:\inetpub\wwwroot\appnamehere\' hit unexpected managed exception, exception code = '0xe0434352'. First 30KB characters of captured stdout and stderr logs: Unhandled exception. System.IO.FileNotFoundException: Could not find file 'C:\inetpub\wwwroot\appnamehere\appsettings..json'. File name: 'C:\inetpub\wwwroot\appnamehere\appsettings..json' at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle) at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at System.IO.StreamReader.ValidateArgsAndOpenPath(String path, Encoding encoding, Int32 bufferSize) at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks) at System.IO.File.InternalReadAllText(String path, Encoding encoding) at System.IO.File.ReadAllText(String path) at appnamehere.Services.Configurations.DatabaseConfigProvider.FetchConfiguration() in C:\Development\solutionNameHere\appnamehere.Services.Configurations\DatabaseConfigProvider.cs:line 25 at appnamehere.Services.Configurations.DatabaseConfigProvider.Load() in C:\Development\solutionNameHere\appnamehere.Services.Configurations\DatabaseConfigProvider.cs:line 20 at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList
1 providers) at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build() at appnamehere.Services.Configurations.AppConfiguration.GetCurrentSettings() in C:\Development\solutionNameHere\appnamehere.Services.Configurations\AppConfiguration.cs:line 34 at appnamehere.Services.Configurations.AppConfiguration.get_Current() in C:\Development\solutionNameHere\appnamehere.Services.Configurations\AppConfiguration.cs:line 24 at appnamehere.Services.Data.Implementation.TitanDbContext.OnConfiguring(DbContextOptionsBuilder optionsBuilder) in C:\Development\solutionNameHere\appnamehere.Services.Data.Implementation\TitanDbContext.cs:line 17 at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance() at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance() at Microsoft.EntityFrameworkCore.Internal.InternalAccessorExtensions.GetService[TService](IInfrastructure
1 accessor) at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor) at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.get_DatabaseCreator() at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated() at appnamehere.Startup..ctor(IConfiguration configuration) in C:\Development\solutionNameHere\appnamehere\Startup.cs:line 27 --- End of stack trace from previous location where exception was thrown --- at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass12_0.b__0(HostBuilderContext context, IServiceCollection services) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() at appnamehere.Program.Main(String[] args) in C:\Development\solutionNameHere\appnamehere\Program.cs:line 20
所以我去了那个位置,那里我所有的应用程序设置文件都是 appsettings.json 和 appsettings.Development.json。 然后我去看看哪里出错了。
public class Program
{
public static void Main(string[] args)
{
try
{
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var host = CreateHostBuilder(args, env).Build();
host.Run();
}
catch (Exception ex)
{
throw;
}
finally
{
NLog.LogManager.Shutdown();
}
}
public static IHostBuilder CreateHostBuilder(string[] args, string env) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.ConfigureAppConfiguration((IConfigurationBuilder builder) =>
{
builder.Sources.Clear();
builder.SetBasePath(Directory.GetCurrentDirectory());
builder.AddDataBaseProvider();
builder.AddJsonFile($"appsettings.{env}.json");
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.ClearProviders();
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
logging.AddEventSourceLogger();
logging.AddFile(hostingContext.Configuration.GetSection("Logging"));
});
}
我尝试硬编码 builder.AddJsonFile($"appsettings.{env}.json");
以明确引用 appsettings.Development.json,但错误仍然存在。
如何让应用程序在我发布后看到 appsettings.Development.json 文件?当我 运行 通过 Visual Studio.
时,它工作得很好我发现了问题。该站点的应用程序池使用默认的 ApplicationPoolIdentity 而不是我的本地用户。所以它没有读取用户环境变量,因此它在 Visual Studio 中工作但不是通过 IIS。
我的解决方案是更改
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
至
var env = "name-Of-File"