다음을 통해 공유


CloudStorageAccount.Parse is your friend

When I started writing my first Azure app, one of the first issues I ran into was dealing with Azure storage -- I couldn't figure out how to set it up in my code so I could use it. I found this nice article about how to structure your connection strings, but what it didn't tell me was where to put it, how to get it out of there and where to pass it when I had it to actually instantiate a storage account.

Whichever connection string you are using, the place to put it is in the settings page of your Role. Give it a name, any name (in this case StorageCxnString), and set the type to ConnectionString.

Then in code, it is simple to retreive it, and pass it to, CloudStorageAccount.Parse, to do the magic. 

private const string StorageCxnString = "StorageCxnString";  

internal static CloudStorageAccount GetStorageAccount()
{
    string storageConnection = RoleEnvironment.GetConfigurationSettingValue(StorageCxnString);

    CloudStorageAccount _storageAccount = CloudStorageAccount.Parse(storageConnection);

    return _storageAccount;
}

Now, when you want to move from Development Storage to real Azure Storage, you just change the connection string in the Role Settings property page, and the code still works. Hopefully, this will save someone out there a headache.

Comments

  • Anonymous
    November 07, 2011
    Ax^2+Bxy+Cy^2+Dx+Ey+F=0. The Y= -Bx+E-sq (BX+E)^2-4(Ax^2+Ey+F)/2c