如何让 .Net Core 3.1 查看我的 appsettings 文件

3
我使用.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工作线程过早退出。该应用程序遇到意外的托管异常,异常代码为'0xe0434352'。所捕获的标准输出和标准错误日志的前30KB字符如下:未处理的异常。System.IO.FileNotFoundException: 找不到文件 'C:\inetpub\wwwroot\appnamehere\appsettings..json'。文件名:'C:\inetpub\wwwroot\appnamehere\appsettings..json' 。 在 C:\Development\solutionNameHere\appnamehere.Services.Configurations\DatabaseConfigProvider.cs 的第25行,调用方法appnamehere.Services.Configurations.DatabaseConfigProvider.FetchConfiguration()读取配置。在 C:\Development\solutionNameHere\appnamehere.Services.Configurations\DatabaseConfigProvider.cs 的第20行,调用方法appnamehere.Services.Configurations.DatabaseConfigProvider.Load()加载配置。 在IList的提供程序中创建Microsoft.Extensions.Configuration.ConfigurationRoot。在C:\Development\solutionNameHere\appnamehere\Services\Configurations\AppConfiguration.cs的第34行,调用方法appnamehere.Services.Configurations.AppConfiguration.GetCurrentSettings()获取当前设置。在C:\Development\solutionNameHere\appnamehere\Services\Configurations\AppConfiguration.cs的第24行,调用属性appnamehere.Services.Configurations.AppConfiguration.Current获取当前设置。在C:\Development\solutionNameHere\appnamehere\Services\Data\Implementation\TitanDbContext.cs的第17行,调用方法appnamehere.Services.Data.Implementation.TitanDbContext.OnConfiguring(DbContextOptionsBuilder optionsBuilder)指定数据库连接字符串。在Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()的第一次调用之后,一个异常被抛出,表明文件C:\inetpub\wwwroot\appnamehere\appsettings..json未找到。

于是我去了那个位置,那里有我的所有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中运行时它完美运行。


如果您想要发布网站,那么您的 IIS 机器上必须安装 .NET Core 3.1。如果没有安装,请下载 .NET Core 3.1 并进行安装。下载链接:https://dotnet.microsoft.com/download/dotnet-core/3.1 - aayushkamboj
@aayushkamboj 是的,我已经安装了 .net core 3.1。 - spmoolman
检查事件查看器中的任何错误并将其添加到问题中。 - Manish
添加 ASPNETCORE_ENVIRONMENT = Development,然后运行 iisreset 命令。 - Manish
@ManishTiwari 我正在尝试弄清楚为什么有两个点。ASPNETCORE_ENVIRONMENT被设置为 "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } - spmoolman
显示剩余5条评论
1个回答

0
我找到了问题所在。该站点的应用程序池使用的是默认的ApplicationPoolIdentity而不是我的本地用户。因此,它无法读取用户环境变量,因此在Visual Studio中可以工作但不能通过IIS工作。
我的解决方案是要进行更改。
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

var env = "name-Of-File"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接