繁体   English   中英

部署到 Azure 服务应用程序后 ConfigurationBuilder.Build() 中的 String Null

[英]String Null in ConfigurationBuilder.Build() after Deployment to Azure Service Apps

我正在尝试将 .NET Core 2.1 应用程序部署到 Azure 服务应用程序。 我能够在本地成功运行我的解决方案,但是当我将应用程序部署到 Azure 服务应用程序时,当我在应用程序上调用dotnet /home/site/wwwroot/MyApplication.dll时,我开始收到以下错误。

Unhandled Exception: System.ArgumentNullException: String reference not set to an instance of a String.
Parameter name: s
   at System.Text.Encoding.GetBytes(String s)
   at MyApplication.Startup.ConfigureServices(IServiceCollection services) in /home/vsts/work/1/s/MyApplication/Startup.cs:line 51
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at MyApplication.Program.Main(String[] args) in /home/vsts/work/1/s/MyApplication/Program.cs:line 23
   at MyApplication.Program.<Main>(String[] args)
bash: line 1:  1096 Aborted                 (core dumped) dotnet /home/site/wwwroot/MyApplication/MyApplication.dll

这似乎在我的 Startup.cs 方法的第 51 行的ConfigureServices方法中有一个空引用。 这与我使用_configuration = builder.Build()构建配置的行相关


public IConfiguration _configuration { get; set; }

public Startup(IConfiguration configuration, IHostingEnvironment hostingEnvironment)
{
    _configuration = configuration;
    _hostingEnvironment = hostingEnvironment;
}

public void ConfigureServices(IServiceCollection services)
{
    var builder = new ConfigurationBuilder()
                      .SetBasePath(_hostingEnvironment.ContentRootPath)
                      .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                      .AddEnvironmentVariables();

    _configuration = builder.Build();  // this is line 51

    var appSettingsSection = _configuration.GetSection("AppSettings");
    ...
}

我唯一能想到的就是我的 appsettings.json 转换。 在开发中,我指向我的开发服务器,在生产中,我指向生产。 因此,在构建之后,我设置了一个文件转换来进行此更改。

但是,我已经从应用服务中提取了生成的 appsettings.json,将它插入到我的本地 appsettings.json 中,它仍然可以编译并运行得很好。

这是正在部署的 appsettings.json。 除了服务器名称和用户名/密码字符串外,它完全相同。

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AppSettings": {
    "SecretKey": "MySecretKey",
    "SuperAdminEmail": "myemail@domain.com"
  },
  "ConnectionStrings": {
    "AuthDatabase": "Server=prod.mydomain.com;Database=Authentication;User Id=myUser;Password=Password1!;TrustServerCertificate=true"
  },
  "AllowedHosts": "*"
}

我看不出是什么导致了这种情况。 我尝试将其部署到 AWS Lightsail Ubuntu 18.04 实例以查看是否有效。 有时我会遇到同样的错误,但是,经过足够多的尝试终止服务、重新启动服务器和其他项目后,我让它运行了 10 分钟,然后再次崩溃。

我不擅长日志记录,尤其是在 Azure 应用程序服务上……我如何追踪 WHAT 字符串为空,以及我可以做些什么来验证它是否具有任何必要的值?

正如您在评论中提到的,AppService 上_hostingEnvironment.ContentrootPath的值为“ /home ”,这是问题所在,因为您的应用程序由于 AppService 引用的路径错误而无法读取某些内容。

您可以按照SO 帖子更改 ContentRootPath ,并根据您的要求相应地选择方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM