我正在尝试以这种方式(在使用解决方案资源管理器选项“添加连接的服务...”添加它之后)检索到config.json文件中定义的Azure存储帐户的连接字符串:
"MicrosoftAzureStorage": {
"almacen_AzureStorageConnectionString": "DefaultEndpointsProtocol=https;AccountName=almacen;AccountKey=pLrQwW......"
}
遵循Microsoft的说明(与MVC 5配合得很好),您可以执行以下操作来检索连接字符串:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("almacen_AzureStorageConnectionString"));
但是CloudConfigurationManager.GetSetting(...)始终返回null。当在Inmediate窗口中调试并尝试时,我得到此调用的输出:
?CloudConfigurationManager.GetSetting("almacen_AzureStorageConnectionString")
Exception thrown: "System.IO.FileNotFoundException" in mscorlib.dll
Exception thrown: "System.IO.FileNotFoundException" in mscorlib.dll
Getting "almacen_AzureStorageConnectionString" from ServiceRuntime: FAIL.
Getting "almacen_AzureStorageConnectionString" from ConfigurationManager: FAIL.
我正在使用RC1版本,并且包含对Microsoft.Extensions.Configuration和Microsoft.Extensions.Configuration.Json包的引用。
编辑
对于下面的评论,我认为CloudConfigurationManager无法从config.json获取连接字符串...因此,从config.json中将连接字符串从azure存储中检索到正确存储的正确过程是什么?对话框“添加已连接的服务...”离开了吗?
有点晚了,但是自从我提出这个问题以来,有人认为实现一个更简单的方法来检索连接字符串是明智的。因此,我将回答我自己的问题,因为现在您可以执行以下操作:
string conn = Configuration.GetConnectionString("ExampleConnection");
如果您之前已在appsettings.json中以这种方式声明,则在ConfigureServices方法中:
"ConnectionStrings": {
"ExampleConnection": "blah,blah..."
},
注意:您必须添加对Microsoft.Extensions.Configuration的引用,此静态方法才能正常工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句