代码之家  ›  专栏  ›  技术社区  ›  tRuEsAtM

使用Azure应用程序服务的应用程序设置导致HTTP 500错误

  •  0
  • tRuEsAtM  · 技术社区  · 6 年前

    我正在使用共享层将我的.NET核心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>
    

    enter image description here

    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

    1 回复  |  直到 6 年前
        1
  •  1
  •   Jerry Liu Phantom    6 年前

    对于.NET核心web应用程序,我们通常在 appsettings.json

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

    获取价值appsetting.json,利用注入的IConfiguration对象。

    1. 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. 中的配置依赖项注入启动.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