3

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?

1

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Your solution can work. But I still do not like it. If you are NodeJS, you can simply put everything into a file and parse that file as a JSON object. Then you can get what you want easily. In my opinion, this framework is trying to hide steps which will make people very hard to trace back what is the issue is.
-3

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.

5 Comments

Because now you have "magic values" in your file, and if those values ever change, you need to recompile and redeploy your website. Further, ASP.NET 5 has an appSettings.json file.
Actually, the official documentation mentions the way to pass in connection string without config file in "Parse the connection string" section.
@Zhaoxing Lu I used that and it simply failed. Where should I put "StorageConnectionString". Have you tried your proposal method before you say it?
Certainly. This is the simplest way to pass in storage connection string to the client library. Could you share your code here so that I can take a look?
CloudStorageAccount is for obsolete NuGet package...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.