如何利用静态类包装配置值?

时间:2018-09-05 14:42:31

标签: c# .net azure visual-studio-2017 azure-functions

我正在尝试使用静态类检索配置值:

public static class Configurator
{
    public static string HashesConnectionString { get; } = Environment.GetEnvironmentVariable("HashesConnectionString");
    public static string SftpUser { get; } = Environment.GetEnvironmentVariable("SftpUser");}

当尝试从一个单独的类中检索它们时,我遇到一个非常尴尬的错误:

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'MyHandler.Configuration.Configurator' threw an exception.
  Source=MyHandler
  StackTrace:
   at MyHandler.Configuration.Configurator.get_HashesConnectionString() in C:\Users\John.S\source\repos\MyHandler\MyHandler\Configuration\Configurator.cs:line 11
   at MyHandler.OnMumboCaseBlobTriggered.RunAsync(CloudBlockBlob blob, String name, TraceWriter log) in C:\Users\John.S\source\repos\MyHandler\MyHandler\Functions\OnMumboCaseBlobTriggered.cs:line 31
   at Microsoft.Azure.WebJobs.Host.Executors.VoidMethodInvoker`2.InvokeAsync(TReflected instance, Object[] arguments)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.<InvokeAsync>d__9.MoveNext()

Inner Exception 1:
ArgumentNullException: Value cannot be null.
Parameter name: String

最初,我以为Environment.GetEnvironmentVariable()发生了一些有趣的事情,但事实并非如此,当我通过硬编码的配置值对这种假设进行测试时,就像这样:

public static class Configurator
{
   public static string HashesConnectionString {get;} = "my hardcoded value"
}

但是异常仍然存在!

我的静态课程有什么问题?我使用了不正确的课程吗?

在此示例中发生此异常:

public static void RunAsync([BlobTrigger("awesomeBlobber", Connection = "StorageConnectionString")] CloudBlockBlob blob, string name, TraceWriter log)
{
               var connString = Configuration.Configurator.HashesConnectionString;

}

0 个答案:

没有答案