So I\'m parsing a connection string for an Azure Storage Account and when I get to the page of the app that uses the connection string, the compiler catches an exception sta
The reason you're running into this error is because you're asking CloudStorageAccount.Parse
method to literally parse "StorageConnectionString"
string instead of the value of this setting stored in app.config file. What you need to do instead is read the value of this setting from the config file. For example, in a console application I would do something like this:
var appSettingsReader = new AppSettingsReader();
var connectionString = (string) appSettingsReader.GetValue("StorageConnectionString", typeof(string));
CloudStorageAccount storageaccount = CloudStorageAccount.Parse(connectionString);
I had to add a reference for System.Configuration
assembly to do so.
Add a reference to System.Configuration.dll and add using System.Configuration;
in the file.
Then change your first line to this:
CloudStorageAccount storageaccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
You need to get the value, not just pass the key to Parse.
Your "StorageConnectionString"
should be in the format of:
DefaultEndpointsProtocol=[http|https];AccountName=<YourAccountName>;AccountKey=<YourAccountKey>'
as described here
Also, use CloudConfigurationManager
to obtain your connection string:
string connectionString = CloudConfigurationManager.GetSetting("StorageConnectionString");
This gives the added benefit of either using app.config/web.config when your app is running locally or accessing your app setting in azure when running in the cloud. See this link
Then you should beable to parse the connection string and have the benefit of not having to modify app.config/web.config settings between development & production environments.