Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ramonsmits/baf653a1d5b7dc3c6a54b56277dd26da to your computer and use it in GitHub Desktop.

Select an option

Save ramonsmits/baf653a1d5b7dc3c6a54b56277dd26da to your computer and use it in GitHub Desktop.
NServiceBus 6 SQL Transport Multi-Instance mode support for connection strings in app.config with legacy 'queue schema' argument
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Data.SqlClient;
namespace NServiceBus
{
using Transport.SQLServer;
public static class SqlServerTransportConfigurationExtensions
{
const string DefaultConnectionString = "NServiceBus/Transport";
const string Prefix = "NServiceBus/SqlTransport/LegacyMultiInstance/"; // Previously `NServiceBus/Transport/*`
public static void ConfigureConfigurationStrings(this TransportExtensions<SqlServerTransport> transport)
{
var item = ConfigurationManager.ConnectionStrings[DefaultConnectionString];
if (item == null) throw new Exception($"Connection string '{DefaultConnectionString}' not set.");
var defaultConnectionString = item.ConnectionString;
var values = new Dictionary<string, string>();
foreach (ConnectionStringSettings i in ConfigurationManager.ConnectionStrings)
{
var QueueSchemaKey = "Queue Schema";
if (!i.Name.StartsWith(Prefix)) continue;
var queueName = i.Name.Substring(Prefix.Length);
var b = new DbConnectionStringBuilder { ConnectionString = i.ConnectionString };
if (b.ContainsKey(QueueSchemaKey))
{
transport.UseSchemaForEndpoint(endpointName: queueName, schema: (string)b[QueueSchemaKey]);
transport.UseSchemaForQueue(queueName: queueName, schema: (string)b[QueueSchemaKey]);
b.Remove(QueueSchemaKey);
}
values[queueName] = b.ToString();
}
transport.EnableLegacyMultiInstanceMode(async address =>
{
var connectionString = values.ContainsKey(address)
? values[address]
: defaultConnectionString;
var connection = new SqlConnection(connectionString);
await connection.OpenAsync().ConfigureAwait(false);
return connection;
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment