[英]How to make ASP.NET Core 3.1 running as a Windows Service see its appsettings file
[英]How to get .Net Core 3.1 to see my appsettings files
我使用 .net core 3.1 創建了一個 api,就像這些東西一樣,它可以在我的機器上運行。 但是當我嘗試將其發布到 IIS 時,它也無法在我的機器上運行。 我必須使用 IIS 作為我的要求之一。 所以錯誤是:
事件查看器錯誤如下:
具有物理根目錄 'C:\\inetpub\\wwwroot\\appnamehere\\' 的應用程序 '/LM/W3SVC/2/ROOT' 未能加載 coreclr。 異常消息:CLR 工作線程提前退出
具有物理根目錄 'C:\\inetpub\\wwwroot\\appnamehere\\' 的應用程序 '/LM/W3SVC/2/ROOT' 遇到意外的托管異常,異常代碼 = '0xe0434352'。 捕獲的 stdout 和 stderr 日志的前 30KB 字符:未處理的異常。 System.IO.FileNotFoundException: 找不到文件“C:\\inetpub\\wwwroot\\appnamehere\\appsettings..json”。 文件名:'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 () 在 C:\\Development\\solutionNameHere\\appnamehere.Services.Configurations\\DatabaseConfigProvider.cs:line 25 at appnamehere.Services.Configurations.DatabaseConfigProvider.Load() 在 C:\\Development\\solutionNameHere\\appnamehere.Services.Confi gurations\\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 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) 在 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 --- 從上一個拋出異常的位置開始的堆棧跟蹤結束 --- 在 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) 在 Microsoft.Extensions.DependencyInjection。 ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] 參數 ) 在 Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services) 在 Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass12_0.b__0(HostBuilderContext context, IServiceCollection services) 在 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"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.