Prerequisites
An instance of SQL Server Express installed and accessible as .
.
At startup each endpoint will create its required SQL assets including databases, tables, and schemas.
The database created by this sample is called SQLServerTruncate
.
Running the project
- Start both the Sender and Receiver projects.
- Press c to send a command, or e to publish an event to the receiver endpoint.
- The receiver endpoint will handle the messages in the matching handler.
Code walk-through
When the endpoint starts up, it runs all INeedInitialization
instances. In this sample, the TruncateTableAtStartup
class is used to truncate the table if it exists at startup.
public class TruncateTableStartupFeature : Feature
{
public TruncateTableStartupFeature()
{
EnableByDefault();
}
protected override void Setup(FeatureConfigurationContext context)
{
var transportInfrastructure = context.Settings.Get<TransportInfrastructure>();
var endpoint = new NServiceBus.Routing.EndpointInstance("Samples.SqlServer.TruncateReceiver");
var connectionString = @"Data Source=.\SqlExpress;Database=SQLServerTruncate;Integrated Security=True;Max Pool Size=100";
SqlHelper.TruncateMessageTable(connectionString, transportInfrastructure.ToTransportAddress(LogicalAddress.CreateRemoteAddress(endpoint)));
}
}
Configure the SQL Server transport
var transport = endpointConfiguration.UseTransport<SqlServerTransport>();
var connection = @"Data Source=.\SqlExpress;Database=SQLServerTruncate;Integrated Security=True;Max Pool Size=100";
transport.ConnectionString(connection);
transport.Routing().RouteToEndpoint(typeof(MyCommand), "Samples.SqlServer.TruncateReceiver");