NServiceBus on Sql Azure, sometimes stuff just works

It just works

Microsoft has put a tremendous amount of effort into Sql Azure to ensure that it works the same way as Sql Server on premises does. The fruits of all this labor are now reaped by us, in order to persist NServiceBus data, such as subscriptions and saga’s for example, on Sql Azure all you got to do is change your connection string.

To show this off, I’ve included a version of the Starbucks sample in the trunk of NServiceBus which has been configured to store it’s information on Sql Azure. You may recall this sample from my previous article so I’m not going to explain the scenario again, I’ll just highlight the differences.

Configuring the sample

In order to configure NServiceBus for Sql Azure, you have to configure it as you would for Sql Server using the DBSubscriptionStorage and the NHibernateSagaPersister configuration settings for respectively subscriptions and saga’s:

Configure.With()
    .Log4Net()
    .StructureMapBuilder(ObjectFactory.Container)

    .AzureConfigurationSource()
    .AzureMessageQueue().JsonSerializer()
    .DBSubcriptionStorage()
    .Sagas().NHibernateSagaPersister().NHibernateUnitOfWork()

    .UnicastBus()
    .LoadMessageHandlers()
    .IsTransactional(true)
    .CreateBus()
    .Start();

In your application configuration file you also need to include the NHibernate connection details using the DBSubscriptionStorageConfig and NHibernateSagaPersisterConfig configuration sections.

<section name="DBSubscriptionStorageConfig"
         type="NServiceBus.Config.DBSubscriptionStorageConfig, NServiceBus.Core" />
<section name="NHibernateSagaPersisterConfig"
         type="NServiceBus.Config.NHibernateSagaPersisterConfig, NServiceBus.Core" />

Furthermore you need to provide the details for your NHibernate connection, like the connection provider, the driver, the sql dialect and finally your connection string. Be sure to format your connection string using the sql azure recognized format and naming conventions.

<DBSubscriptionStorageConfig>
  <NHibernateProperties>
    <add Key="connection.provider"
          Value="NHibernate.Connection.DriverConnectionProvider"/>
    <add Key="connection.driver_class"
          Value="NHibernate.Driver.SqlClientDriver"/>
    <add Key="connection.connection_string"
          Value="Server=tcp:[yourserver].database.windows.net;Database=NServiceBus;User ID=[accountname]@[yourserver];Password=[accountpassword];Trusted_Connection=False;Encrypt=True;"/>
    <add Key="dialect"
          Value="NHibernate.Dialect.MsSql2005Dialect"/>
  </NHibernateProperties>
</DBSubscriptionStorageConfig>

<NHibernateSagaPersisterConfig>
  <NHibernateProperties>
    <add Key="connection.provider"
          Value="NHibernate.Connection.DriverConnectionProvider"/>
    <add Key="connection.driver_class"
          Value="NHibernate.Driver.SqlClientDriver"/>
    <add Key="connection.connection_string"
          Value="Server=tcp:[yourserver].database.windows.net;Database=NServiceBus;User ID=[accountname]@[yourserver];Password=[accountpassword];Trusted_Connection=False;Encrypt=True;"/>
    <add Key="dialect"
          Value="NHibernate.Dialect.MsSql2005Dialect"/>
  </NHibernateProperties>
</NHibernateSagaPersisterConfig>

Alright, that was it, validate your other settings, like the azure storage account used as a transport mechanism and hit F5, it just works!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: