【问题标题】:Using Application settings of Azure App Service resulting into an HTTP 500 error使用 Azure 应用服务的应用程序设置导致 HTTP 500 错误
【发布时间】:2018-10-30 18:35:14
【问题描述】:

我正在使用共享层将我的 .NET Core Web 应用程序部署到 Azure。

下面是我的 app.config 文件,

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="SASToken" value="" />
    <add key="StorageAccountPrimaryUri" value="" />
    <add key="StorageAccountSecondaryUri" value="" />
  </appSettings>
</configuration>

在 Azure 门户上的应用程序设置下,我更新了以下内容,

但是,当我访问我的 API 时,我收到 Http 500 错误,并显示以下异常详细信息,

System.ArgumentException: The argument must not be empty string. Parameter name: sasToken at Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility.AssertNotNullOrEmpty(String paramName, String value) at Microsoft.WindowsAzure.Storage.Auth.StorageCredentials..ctor(String sasToken) at ProfileVariable.DataAccessor.AzureTableStorageAccount.TableStorageAccount.ConfigureAzureStorageAccount() in C:\Users\sranade\Source\Repos\ProfileVariableService\ProfileVariable.DataAccessor\AzureTableStorageAccount\TableStorageAccount.cs:line 22

【问题讨论】:

  • 这是 .NET Framework 还是 .NET Core 应用程序?您如何在代码中访问此值?
  • 这是一个 .NET Core 应用程序。 var storageCred = new StorageCredentials(ConfigurationManager.AppSettings["SASToken"]); 这就是我访问代码中值的方式。
  • 我相信门户应用程序设置会更新(在运行时)appsettings.json,但不会更新任何app.config 值。
  • 在发布到 azure 之前,您应该将 ConfigurationManager.AppSettings["SASToken"] 更改为使用:Environment.GetEnvironmentVariable("SASToken",EnvironmentVariableTarget.Process)
  • @Sameer 如果我的解决方案不起作用,请随意询问,否则您可以接受它来结束您的问题吗?

标签: c# azure azure-web-app-service azure-api-apps


【解决方案1】:

对于 .NET Core Web 应用,我们通常将设置放在 appsettings.json 中。

{
  "SASToken": "TOKENHERE",
  "StorageAccountPrimaryUri":"CONNECTIONSTRING",
  ...
}

要在 appsetting.json 中获取值,请利用注入的 IConfiguration 对象。

  1. 使用 Interface 重构您的代码并添加 IConfiguration 字段。

    public interface ITableStorageAccount { string Method(); }
    
    public class TableStorageAccount : ITableStorageAccount
    {
    
        private readonly IConfiguration Configuration;
    
        public TableStorageAccount(IConfiguration configuration)
        {
            Configuration = configuration;
        }
    
        // an example return table storage uri
        public string Method()
        {
            string cre = Configuration["SASToken"];
            CloudTableClient table = new CloudTableClient(new Uri("xxx"), new StorageCredentials(cre));
            return table.BaseUri.AbsolutePath;
        }
    }
    
  2. 在 startup.cs 中配置依赖注入

    public void ConfigureServices(IServiceCollection services)
    {
        ...
        services.AddSingleton<ITableStorageAccount, TableStorageAccount>();
    }
    
  3. 在控制器中使用服务。

    private readonly ITableStorageAccount TableStorageAccount;
    
    public MyController(ITableStorageAccount TableStorageAccount)
    {
        this.TableStorageAccount = TableStorageAccount;
    }
    

在 Program.cs 模板中。

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();

CreateDefaultBuilder() 负责加载 appsetting.json 等配置,详情请参阅docs

【讨论】:

  • 我要补充一点,通过这种方式,您将能够以您想要的方式通过 Azure 门户覆盖设置
猜你喜欢
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
  • 2015-11-26
  • 1970-01-01
  • 1970-01-01
  • 2020-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多