如何获得.Net Core 3.1来查看我的appsettings文件

问题描述 投票:0回答:1

我使用.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'。我在事件查看器中找到了它。

事件查看器错误如下:

具有物理根目录'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”在System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)在System.IO.FileStream.CreateFileOpenHandle处(FileMode模式,FileShare共享,FileOptions选项)在System.IO.FileStream..ctor处(字符串路径,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,FileOptions选项)在System.IO.StreamReader.ValidateArgsAndOpenPath(字符串路径,编码编码,Int32 bufferSize)在System.IO.StreamReader..ctor处(字符串路径,编码编码,布尔型detectEncodingFromByteOrderMarks)在System.IO.File.InternalReadAllText(字符串路径,编码编码)在System.IO.File.ReadAllText(字符串路径)位于appnamehere.Services.Configurations.DatabaseConfigProvider.FetchConfiguration()在C:\ Development \ solutionNameHere \ appnamehere.Services.Configurations \ DatabaseConfigProvider.cs:第25行位于appnamehere.Services.Configurations.DatabaseConfigProvider.Load()在C:\ Development \ solutionNameHere \ appnamehere.Services.Configurations \ DatabaseConfigProvider.cs:line 20在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](IInfrastructure1访问器)在Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService [TService](IInfrastructure`1访问器)处在Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.get_DatabaseCreator()在Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated()在appnamehere.Startup..ctor(IConfiguration配置)在C:\ Development \ solutionNameHere \ appnamehere \ Startup.cs:第27行---从之前引发异常的位置开始的堆栈结束跟踪---在Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider提供程序)在Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider提供程序,类型instanceType,Object []参数)在Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(类型startupType,HostBuilderContext上下文,IServiceCollection服务)在Microsoft.AspNetCore.Hosting.GenericWebHostBuilder。<> c__DisplayClass12_0.b__0(HostBuilderContext上下文,IServiceCollection服务)在Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()在Microsoft.Extensions.Hosting.HostBuilder.Build()在appnamehere.Program.Main(String [] args)在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运行它时,它可以完美运行。

c# appsettings asp.net-core-3.1
1个回答
0
投票

我发现了问题。该站点的应用程序池使用默认的ApplicationPoolIdentity,而不是我的本地用户。因此它没有读取用户环境变量,因此它可以在Visual Studio中工作,但不能通过IIS。

我的解决方案是更改

var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

to

var env = "name-Of-File"
© www.soinside.com 2019 - 2024. All rights reserved.