Reputation: 10257
I am creating an ASP.Net 5 WebAPI application in Visual Studio 2015 and I need use Azure Blob.
To use Azure blob, from the official document: https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/, you need put a key-value pair in <appSettings />
in an app.config
or web.config
file like this:
<appSettings>
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key" />
</appSettings>
But the problem is if you are using ASP.Net 5, there is no such file called app.config
or web.config
.
So If I am working with ASP.Net 5, where should I put the StorageConnectionString
?
Upvotes: 3
Views: 3363
Reputation: 18465
ASP.NET Core provides a variety of different configuration options. Application configuration data could come from files(e.g. JSON, XML, etc.), environment variables, in-memory collections and so on.
It works with the options model so that you could inject strongly typed settings into your application. You also could create custom configuration providers, which could bring you with more flexibility and extensibility.
According to your requirement, you could follow the steps below to achieve your purpose:
Create a section called AzureStorageConfig in your appsetting.json:
"AzureStorageConfig": {
"AccountName": "<yourStorageAccountName>",
"AccountKey": "<yourStorageAccountKey>"
}
Create a class called AzureStorageConfig like this:
public class AzureStorageConfig
{
public string AccountName { get; set; }
public string AccountKey { get; set; }
}
Then configure the services in your Startup.cs like this:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
// Setup options with DI
services.AddOptions();
services.Configure<AzureStorageConfig>(Configuration.GetSection("AzureStorageConfig"));
}
Then access it through a controller like this:
private AzureStorageConfig _storageConfig;
public HomeController(IOptions<AzureStorageConfig> config)
{
_storageConfig = config.Value;
}
For more details, you could refer to this Tutorial.
Upvotes: 8
Reputation: 10257
The solution is simple. You do not have to go through the tedious/ridiculous steps mentioned in their official tutorial.
var storageAccount = new CloudStorageAccount(
new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials(
"{account_name}",
"key"), true);
I have no idea why Microsoft is trying their best to complicate people's life.
Upvotes: -3