I'm having a bit of trouble making Azure work on the dev server. I have a Silverlight app that I would like to connect to Azure, so I'm exposing a REST API from the Web Role.
Here is my service class:
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ExpenseService
{
private ExpenseDataSource expenseData = new ExpenseDataSource();
[OperationContract]
[WebGet(UriTemplate="expenses", ResponseFormat=WebMessageFormat.Xml)]
public List<String> GetExpenses()
{
return expenseData.Select().ToList();
}
}
The type initialization of ExpenseDataSource
is failing:
public class ExpenseDataSource
{
private static CloudStorageAccount storageAccount;
private ExpenseTableContext context;
static ExpenseDataSource()
{
CloudStorageAccount.SetConfigurationSettingPublisher(
(configName, configSettingPublisher) =>
{
string connectionString = RoleEnvironment.GetConfigurationSettingValue(configName);
configSettingPublisher(connectionString);
}
);
storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
CloudTableClient.CreateTablesFromModel(typeof(ExpenseTableContext), storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials);
}
// ...
}
The error is:
SEHException was caught
External component has thrown an exception.
I'm not sure what I'm doing wrong. I'm trying to follow along with this tutorial. The author describes needing to do something differently for non-Azure environments, like NUnit tests. Do I need to do something for running on the dev server? Should I just configure this app to use the real Azure storage, even though it's running off my machine?
Update: If I comment out the ExpenseDataSource
constructor and just use fake data, the service works fine.
Update 2: @Maupertuis answered that I can't set up the storage account from a static initializer. However, this comes directly from a MS Windows Azure code sample:
public class GuestBookEntryDataSource
{
private static CloudStorageAccount storageAccount;
private GuestBookDataContext context;
static GuestBookEntryDataSource()
{
storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
CloudTableClient.CreateTablesFromModel(
typeof(GuestBookDataContext),
storageAccount.TableEndpoint.AbsoluteUri,
storageAccount.Credentials);
}
// ...
}
Could this ever work?