web-dev-qa-db-ja.com

.Net Core 3.1を入手してappsettingsファイルを表示する方法

私は.netコア3.1を使用してAPIを作成しましたが、これらのように、私のマシンで動作します。しかし、IISに発行しようとすると、私のマシンでは機能しません。私はIISを私の要件の1つとして使用する必要があります。したがって、エラーは次のとおりです。

  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ログの最初の30K​​B文字:未処理の例外。 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 System.IO.StreamReaderの.IO.FileStream..ctor(String path、FileModeモード、FileAccessアクセス、FileShare共有、Int32 bufferSize、FileOptionsオプション)at System.IO.StreamReader.ValidateArgsAndOpenPath(String path、Encoding encoding、Int32 bufferSize) ..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.Confi gurations\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- -例外がスローされた前の場所からのスタックトレースの終わり--- at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider、Type instanceType、Object []パラメーター)Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType、HostBuilderContext context、I ServiceCollection 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"));
            });
}
_

Appsettings.Development.jsonを明示的に参照するようにbuilder.AddJsonFile($"appsettings.{env}.json");をハードコーディングしようとしましたが、エラーが解決しませんでした。

公開後、アプリケーションにappsettings.Development.jsonファイルを表示するにはどうすればよいですか? Visual Studioで実行すると、問題なく動作します。

1
spmoolman

問題が見つかりました。サイトのアプリケーションプールは、ローカルユーザーではなく、デフォルトのApplicationPoolIdentityを使用しました。したがって、ユーザー環境変数を読み取らなかったため、Visual Studioでは機能しましたが、IIS経由では機能しませんでした。

私の解決策は変更することでした

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

var env = "name-Of-File"
0
spmoolman