在Azure中将配置解析为IOptions <t>失败

时间:2016-10-09 19:06:08

标签: azure asp.net-core azure-web-sites

我使用IOptions模式将我的配置文件解析为强类型类。

我在ConfigureServices中执行此操作:

services.AddOptions();
services.Configure<StorageSettings>(Configuration.GetSection("StorageSettings"));

StorageSettings类看起来像这样:

public class StorageSettings
{
    public ConnectionStrings ConnectionStrings { get; set; }
}

public class ConnectionStrings
{
    public TableStorageConnectionStrings TableStorage { get; set; }

    public EventHubConnectionStrings EventHubs { get; set; }

    public ServiceBusConnectionStrings ServiceBus { get; set; }
}

public class TableStorageConnectionStrings
{
    public string LogEntriesByAccount { get; set; }
}

public class EventHubConnectionStrings
{
    public string EventHubName { get; set; }

    public string IngestionQueue { get; set; }

    public string AccessKeyName { get; set; }

    public string AccessKeyToken { get; set; }

    public string ServiceNameSpace { get; set; }
}

public class ServiceBusConnectionStrings
{
    public string GlobalQueue { get; set; }
}

我的JSON配置文件如下所示:

{
    "SecuritySettings": {
        "SystemToken": "123"
    },
    "StorageSettings": {
        "ConnectionStrings": {
            "TableStorage": {
                "LogEntriesByAccount": "DefaultEndpointsProtocol=https;AccountName=account;AccountKey=key=="
            },
            "EventHubs": {
                "EventHubName": "myhub",
                "AccessKeyName": "key",
                "AccessKeyToken": "token",
                "ServiceNameSpace": "ns",
                "IngestionQueue": ""
            },
            "ServiceBus": {
                "GlobalQueue": "DISABLED"
            }
        }
    },
    "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
            "Default": "Debug",
            "System": "Warning",
            "Microsoft": "Warning"
        }
    }
}

它在本地运行时有效,并且配置部分和属性被解析得很好。但是在运行i Azure时我遇到了这个错误:

[13:54:40 INF] Starting up API in Production
Application startup exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Failed to convert '@{LogEntriesByAccount=DefaultEndpointsProtocol=https;AccountName=mystore001;AccountKey=KEY====000}' to type 'MyApp.Storage.Configuration.TableStorageConnectionStrings'. ---> System.NotSupportedException: TypeConverter cannot convert from System.String.
   at System.ComponentModel.TypeConverter.GetConvertFromException(Object value)
   at System.ComponentModel.TypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   at System.ComponentModel.TypeConverter.ConvertFromInvariantString(String text)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.ConvertValue(Type type, String value)
   --- End of inner exception stack trace ---
   at Microsoft.Extensions.Configuration.ConfigurationBinder.ConvertValue(Type type, String value)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.BindInstance(Type type, Object instance, IConfiguration config)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.BindProperty(PropertyInfo property, Object instance, IConfiguration config)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.BindNonScalar(IConfiguration configuration, Object instance)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.BindInstance(Type type, Object instance, IConfiguration config)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.BindProperty(PropertyInfo property, Object instance, IConfiguration config)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.BindNonScalar(IConfiguration configuration, Object instance)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.BindInstance(Type type, Object instance, IConfiguration config)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(IConfiguration configuration, Object instance)
   at Microsoft.Extensions.Options.ConfigureFromConfigurationOptions`1.<>c__DisplayClass0_0.<.ctor>b__0(TOptions options)
   at Microsoft.Extensions.Options.ConfigureOptions`1.Configure(TOptions options)
   at Microsoft.Extensions.Options.OptionsCache`1.CreateOptions()
   at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory)
   at System.Threading.LazyInitializer.EnsureInitialized[T](T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory)
   at Microsoft.Extensions.Options.OptionsCache`1.get_Value()
   at Microsoft.Extensions.Options.OptionsManager`1.get_Value()
   at MyApp.Storage.Azure.EventHubAMQPIngestionQueue..ctor(IOptions`1 options, ILoggerFactory loggerFactory) in C:\Workspace\Dev\MyApp\src\MyApp.Storage\Azure\EventHubAMQPIngestionQueue.cs:line 29
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.SingletonCallSite.Invoke(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass12_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
   at MyApp.API.Startup.ConfigureServices(IServiceCollection services) in C:\Workspace\Dev\MyApp\src\MyApp.API\Startup.cs:line 124
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at Microsoft.AspNetCore.Hosting.Internal.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection exportServices)
   at Microsoft.AspNetCore.Hosting.Internal.ConfigureServicesBuilder.<>c__DisplayClass4_0.<Build>b__0(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
Hosting environment: Production
Content root path: D:\home\site\wwwroot
Now listening on: http://localhost:23758
Application started. Press Ctrl+C to shut down.

1 个答案:

答案 0 :(得分:1)

我决定创建一个测试项目,看看我是否可以在一个小而孤立的项目中重新产生这个问题。我不能。

事实证明,我长时间使用的Azure App Service(Web App)实例是罪魁祸首 - 在绝望中我将Azure中失败的项目部署到新的App Service实例并开始工作。

我尝试使用Kudu删除了很多文件夹,但它没有用。

我还比较了所有应用程序设置并验证它们是相同的。

我最终删除了我正在使用的App Service实例,而不是新创建的实例。

我使用的测试项目是这个:https://github.com/martinnormark/ConfigOptionsTest