14

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");
michael__sloan
  • 405
  • 2
  • 4
  • 14
  • I edited the formatting too, which shows that you have a configuration section within the configuration section. That looks like an invalid config file. You sure you copied it into your question correctly? Or maybe it's really like that, which would explain why you're having an issue. – David Makogon Apr 18 '16 at 15:46
  • Are there any ampersand (&) in your StorageConnectionString value? – Fabio Lima Apr 18 '16 at 15:58
  • Not a single ampersand... – michael__sloan Apr 18 '16 at 15:59
  • 1
    Here's a little backstory. I followed [this](https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-tables/) walkthrough. Since I didn't already have an app.config file in my Xamarin.Android solution. I created a new one. Maybe I should've set up the connection string in a different file to begin with. Any ideas? – michael__sloan Apr 18 '16 at 16:04
  • Your posted example also fails to close the appSettings section. Your closing should be – abrown Apr 18 '16 at 16:24
  • I would suggest to try hard coding the connnectionkey in the code and try to run the code if that works than probably it will be easy to compare what is different in case of using app.config. – TusharJ Apr 18 '16 at 16:29
  • Please share your code for creating an instance of `CloudStorageAccount` object. – Gaurav Mantri Apr 18 '16 at 16:30
  • @abrown thanks, but that was just a post typo. – michael__sloan Apr 18 '16 at 16:42
  • @GauravMantri alright, I edited my post to show the instance of CloudStorageAccount. – michael__sloan Apr 18 '16 at 16:43
  • 1
    Hard to guess what "==" could possibly mean. Other than a good reason for this exception. Delete it and try again. – Hans Passant Apr 18 '16 at 16:46
  • @HansPassant nope, it's actually apart of the key that got left on the post after I edited it. I'll get rid of it on the post to avoid confusion. – michael__sloan Apr 18 '16 at 16:48
  • I was really hoping this question had been answered. :( same issue, and no idea why. – Sailing Judo Mar 19 '17 at 06:27

3 Answers3

7

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.

PhatBuck
  • 326
  • 5
  • 15
6

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.

juunas
  • 54,244
  • 13
  • 113
  • 149
0

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.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241