SQL Server Transport Startup Purge Behavior

NuGet Package: NServiceBus.Transport.SqlServer (7-pre)
This page targets a pre-release version. Pre-releases are subject to change and samples are not guaranteed to be fully functional.


An instance of SQL Server Express, accessible as .\SqlExpress.

At startup, each endpoint creates its required SQL assets, including databases, tables, and schemas.

The database created by this sample is called SQLServerTruncate.

Running the project

  1. Start both the Sender and Receiver projects.
  2. Press c to send a command, or e to publish an event to the receiver endpoint.
  3. 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()

    protected override void Setup(FeatureConfigurationContext context)
        var connectionString = @"Data Source=.\sqlsexpress;Database=SQLServerTruncate;Integrated Security=True;Max Pool Size=100";
        SqlHelper.TruncateMessageTable(connectionString, "Samples.SqlServer.TruncateReceiver");

Configure the SQL Server transport

var connection = @"Data Source=.\sqlexpress;Database=SQLServerTruncate;Integrated Security=True;Max Pool Size=100";
var routing = endpointConfiguration.UseTransport(new SqlServerTransport(connection)
    TransportTransactionMode = TransportTransactionMode.SendsAtomicWithReceive
routing.RouteToEndpoint(typeof(MyCommand), "Samples.SqlServer.TruncateReceiver");

Related Articles

Last modified