gpt4 book ai didi

c# - 如何让 .Net Core 3.1 查看我的应用程序设置文件

转载 作者:行者123 更新时间:2023-12-05 07:14:37 26 4
gpt4 key购买 nike

我使用 .net core 3.1 创建了一个 api,就像这些东西一样,它可以在我的机器上运行。但是当我尝试将它发布到 IIS 时,它在我的机器上也不起作用。我必须使用 IIS 作为我的要求之一。所以错误是:

  1. HTTP 错误 500.30 - ANCM 进程中启动失败。我尝试了很多东西,但似乎没有任何效果。我查看了 appsettings 文件以查看是否遗漏了逗号或其他内容,并更改了 IIS 中的设置,以便应用程序池不管理代码并禁用 32 位应用程序选项。
  2. 未处理的异常。 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 文件都是 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"

关于c# - 如何让 .Net Core 3.1 查看我的应用程序设置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59856250/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com