@jbogard I tried to simplify that a bit and created NSeviceBus.Connector.SqlServer (source here) which allows you to easy inject into a controller an instance of IMessageSession
that shares connection and transaction with data access library.
The controller code is not aware of the connector. It just uses the session:
public SendMessageController(IMessageSession messageSession, SqlConnection conn, SqlTransaction trans)
{
this.messageSession = messageSession;
this.conn = conn;
this.trans = trans;
}
[HttpGet]
public async Task<string> Get()
{
await messageSession.Send(new MyMessage())
.ConfigureAwait(false);
await messageSession.Publish(new MyEvent())
.ConfigureAwait(false);
using (var command = new SqlCommand("insert into Widgets default values;", conn, trans))
{
await command.ExecuteNonQueryAsync()
.ConfigureAwait(false);
}
return "Message sent to endpoint";
}
In the setup code:
//Connects to MSMQ transport used by other endpoints
var connectorConfig = new ConnectorConfiguration<MsmqTransport>(
name: "WebApplication",
sqlConnectionString: ConnectionString,
customizeConnectedTransport: extensions => {},
customizeConnectedInterface: configuration =>
{
//Required because connected transport (MSMQ) does not support pub/sub
configuration.EnableMessageDrivenPublishSubscribe(storage);
});
//Routing for commands
connectorConfig.RouteToEndpoint(typeof(MyMessage), "Samples.ASPNETCore.Endpoint");
//Start the connector
connector = connectorConfig.CreateConnector();
await connector.Start();
//Register per-request SQL Server connection
services.UseSqlServer(ConnectionString);
//Register automatic opening of per-request transaction
services.UseOneTransactionPerHttpCall();
//Register per-request connector-based message session based on connection/transaction
services.UseNServiceBusConnector(connector);
There is a sample included in the repo that I intend to eventually move to NServiceBus doco site. My goal is to package the router and the send-only endpoint in such a way that for the user it looks just like an ordinary NServiceBus send-only endpoint. The only difference is the fact that it requires two queues – one in the SQL Server and the other in the external transport. That second queue is used for managing pub/sub in case the external transport does not handle it natively.
Szymon