Asp.net core CloudConfigurationManager.GetSetting() is null

妖精的绣舞 提交于 2021-02-08 19:08:24

问题


I have an asp.net 4 application where it works fine, but I can't get it working for asp.net core. The documentation says that GetSetting looks in either web.config or app.config file. Storage emulator is turned on.

public void ConfigureServices(IServiceCollection services)
{
    AzureConfig.Initialize();
}

public static void Initialize()
{
    //Always null
    var connectionString = CloudConfigurationManager.GetSetting("StorageConnectionString");
}

root web.config file:

<configuration>
    <appSettings>
        <add key="StorageConnectionString" value="UseDevelopmentStorage=true;" />
    </appSettings>
</configuration>

I have these dependencies in my project.json:

"Microsoft.WindowsAzure.ConfigurationManager": "3.2.1",
"WindowsAzure.Storage": "7.1.2",
"AzureSDK2.2DLLs": "1.0.0"

Edit, this doesn't work either:

  {
  "ConnectionStrings": {
    "DefaultConnection": "..."
    "StorageConnectionString": "UseDevelopmentStorage=true"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "StorageConnectionString": "UseDevelopmentStorage=true"
}

回答1:


In ASP.NET core, the settings have been moved out of the web.config and in to appsettings.json. Have a look at the documentation for how it has changed:

https://docs.asp.net/en/latest/fundamentals/configuration.html

Also, have a look at this previous answer to a similar question:

https://stackoverflow.com/a/30580006/2823539



来源:https://stackoverflow.com/questions/38551528/asp-net-core-cloudconfigurationmanager-getsetting-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!