tags:

views:

142

answers:

3

I am trying to access Azure Storage from a console app like this:

CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("myConnectionString");  

where the connection string is something like:

DefaultEndpointsProtocol=https;AccountName=XXX;AccountKey=XXX

but i get an exception:

System.Runtime.InteropServices.SEHException was caught Message=External component has thrown an exception. Source=msshrtmi ErrorCode=-2147467259

StackTrace:

   at RoleEnvironmentGetConfigurationSettingValueW(UInt16* , UInt16* , UInt64 , UInt64* )

   at  Microsoft.WindowsAzure.ServiceRuntime.Internal.InteropRoleManager.GetConfigurationSetting(String name, String& ret)

   at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue(String configurationSettingName)

   at AzureUpload.Program.<Init>b__2(String configName, Func`2 configSetter) in C:\Users\siddjain\Documents\Visual Studio 2010\Projects\ConsoleAplication1\Program.cs:line 51

   at Microsoft.WindowsAzure.CloudStorageAccount.StorageAccountConfigurationSetting..ctor(String configurationSettingName)

   at Microsoft.WindowsAzure.CloudStorageAccount.FromConfigurationSetting(String settingName)

   at AzureUpload.Program.UploadBlob(String directory, String searchPattern, String container) in C:\Users\siddjain\Documents\Visual Studio 2010\Projects\ConsoleApplication1\Program.cs:line 87

InnerException

Do I need to start up azure services or something before running my app?

+2  A: 

FromConfigurationSetting uses the role runtime API, which tries to read configuration settings. That won't work when running outside Windows Azure.

Use CloudStorageAccount.Parse("<myConnectionString>") instead. (Or fetch the config setting from app.config or somewhere else and then pass it into Parse()).

smarx
+2  A: 

As Steve mentioned, you're trying to retrieve settings from the Azure configuration settings, which don't exist in a console app.

If you want to write your code to run in either non-Azure or Azure environments, you can specify a configuration publisher. When in Azure, it's simply a wrapper. Otherwise, you basically redirect to app.config / web.config.

There's a great CodeProject article that demonstrates this. Here's a code snippet from that article. In essence, you'd specify the configuration publisher in your OnStart method:

CloudStorageAccount.SetConfigurationSettingPublisher(
    StorageAccountFactory.GetConfigurationSettingPublisher()
);

In this case, you'd have a factory method deciding where to store/retrieve config settings, based on whether you're in Azure (RoleEnvironment.IsAvailable):

public static Action<string, Func<string,bool>> GetConfigurationSettingPublisher()
{
    if (RoleEnvironment.IsAvailable)
      return (configName, configSetter) => 
    configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
    return (configName, configSetter) => 
    configSetter(ConfigurationManager.AppSettings[configName]);
}
David Makogon
A: 

In the SDK and in the Training Kit there are samples of that.

Nico Ploner