michael__sloan
michael__sloan

Reputation: 425

Settings must be of the form "name=value". No idea what to do

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 stating, "Settings must be of the form "name=value".

Does this mean that I should correct something in the app.config file where I set the appSettings? If so can you immediately spot something wrong with my format that would cause this exception?

<?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <appSettings>
            <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=mykey" />
        <appSettings>
    </configuration>

Here's the code for creating an instance of CloudStorage object:

CloudStorageAccount storageaccount = CloudStorageAccount.Parse ("StorageConnectionString");

        CloudTableClient tableClient = storageaccount.CreateCloudTableClient ();

        CloudTable austinBowlingAthletes = tableClient.GetTableReference ("austinBowlingAthletesTable");

Upvotes: 16

Views: 33803

Answers (3)

juunas
juunas

Reputation: 58723

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.

Upvotes: 6

PhatBuck
PhatBuck

Reputation: 356

Your "StorageConnectionString" should be in the format of:

DefaultEndpointsProtocol=[http|https];AccountName=<YourAccountName>;AccountKey=<YourAccountKey>' as described here

Also, use CloudConfigurationManagerto 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.

Upvotes: 10

Gaurav Mantri
Gaurav Mantri

Reputation: 136146

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.

Upvotes: 0

Related Questions