Sql Server storage of subscriptions for use with Shuttle.Hopper.
Note
The implementation will automatically create the required database schema and tables if they are missing. The identity used to connect to the database must have sufficient permissions to create schemas and tables.
dotnet add package Shuttle.Hopper.SqlServer.SubscriptionConfigure both the Hopper options (inbox URI and subscription message types) and the Sql Server subscription options:
services
.AddHopper(options =>
{
options.Inbox.WorkTransportUri = new Uri("queue://./inbox");
options.Subscription.MessageTypes = new[] { typeof(MyMessage).FullName! };
})
.UseSqlServerSubscription(options =>
{
options.ConnectionString = "server=.;database=shuttle;user id=sa;password=Pass!000";
});The SubscriptionMode option controls how missing subscriptions are handled:
| Mode | Behavior |
|---|---|
Standard |
Registers the subscriber for all configured message types (default) |
Disabled |
Skips subscription registration entirely |
FailWhenMissing |
Throws ApplicationException if any configured message type is not already subscribed |
options.Subscription.Mode = SubscriptionMode.FailWhenMissing;The default JSON settings structure is as follows:
{
"Shuttle": {
"SqlServer": {
"Subscription": {
"ConnectionString": "connection-string",
"Schema": "dbo",
"ConfigureDatabase": true
}
}
}
}| Option | Default | Description |
|---|---|---|
ConnectionString |
(required) | The connection string to use for the Sql Server database. Validated at startup. |
Schema |
dbo |
The schema to use for the subscription tables. Must be a valid SQL identifier matching pattern ^[a-zA-Z_][a-zA-Z0-9_]*$. |
ConfigureDatabase |
true |
A boolean value indicating whether the database schema and tables should be created if they don't exist. |
When ConfigureDatabase is true, the following objects are created automatically using a distributed lock to ensure thread-safe creation:
- Schema: As specified by the
Schemaoption (default:dbo) - Table:
SubscriberMessageTypewith the following structure:
| Column | Type | Constraints |
|---|---|---|
MessageType |
varchar(250) |
NOT NULL, part of PRIMARY KEY |
InboxWorkQueueUri |
varchar(250) |
NOT NULL, part of PRIMARY KEY |
HopperOptions.Inbox.WorkTransportUrimust be set before subscription registration- At least one message type must be configured in
HopperOptions.Subscription.MessageTypes - The
ConnectionStringoption is mandatory and validated at startup - The
Schemaoption is mandatory and must be a valid SQL identifier
The SubscriptionObserver is automatically added to the StartupPipeline during application initialization. When the application starts, it:
- Creates the database schema and
SubscriberMessageTypetable ifConfigureDatabaseistrue - Registers subscriptions for all configured message types based on the
SubscriptionMode - Publishers use
ISubscriptionQuery.GetSubscribedUrisAsync(messageType)to find all subscriber URIs for a given message type