AddServiceConnect
Overview
Section titled “Overview”AddServiceConnect is the dependency-injection entry point. Call it once on your IServiceCollection during composition; pass a builder delegate that selects a transport (for example UseRabbitMQ), configures queues and persistence, and registers pipeline middleware. The method wires up IBus, the configuration interfaces, handler registries, message processors, and the hosted services that drive consumption and timeout polling.
See Getting Started for a walk-through from zero to a running consumer.
Reference
Section titled “Reference”AddServiceConnect
Section titled “AddServiceConnect”public static IServiceCollection AddServiceConnect( this IServiceCollection services, Action<ServiceConnectBuilder> configure);Registers the core ServiceConnect services, handler discovery, message processors, and hosted services with services, using configure to populate transport, queue, persistence, and pipeline settings.
Parameters
services— the service collection to extend.configure— a callback that receives aServiceConnectBuilder. Chain calls toUseRabbitMQ,ConfigureQueues,ConfigurePersistence, and the typedAdd*Filter<T>/Add*Middleware<T>methods inside it to build up the bus configuration.
Returns. The same services instance for chaining.
Remarks. Transport selection is not baked in; pick a transport via extension methods on ServiceConnectBuilder (UseRabbitMQ, etc.). Handler types are discovered from builder.ScanAssemblies(...) when IBusConfiguration.ScanForMessageHandlers is true. Call AddServiceConnect exactly once per service collection.
Builder methods
Section titled “Builder methods”The ServiceConnectBuilder passed to configure exposes the following helpers in addition to the transport and persistence extensions registered by satellite packages:
| Method | Purpose |
|---|---|
ScanAssemblies(params Assembly[]) | Restrict handler discovery to a fixed list of assemblies instead of every loaded assembly. Preferred for deterministic startup and tests. |
AddRegistration(Action<IServiceCollection>) | Run an arbitrary registration callback after the core ServiceConnect services have been registered. Use this to swap framework defaults (e.g. a custom IRequestReplyManager) or to register your own services that depend on ServiceConnect types. |
ConfigureTransport, ConfigureQueues, ConfigurePersistence, ConfigureBus | Mutate the matching configuration interfaces. Validation runs eagerly inside ConfigureTransport (host, retry, shutdown timeouts). Pipeline registrations use the typed builder methods below — there is no public ConfigurePipeline callback. |
AddOutgoingFilter<T>, AddBeforeConsumingFilter<T>, AddOnConsumedSuccessfullyFilter<T>, AddAfterConsumingFilter<T> | Register an IFilter at the corresponding pipeline stage. Filters short-circuit by returning FilterAction.Stop. OnConsumedSuccessfully runs only when the dispatcher chain completed cleanly (Success = true and NotHandled = false); use it for at-most-once side effects that depend on the handler having succeeded. |
AddSendMessageMiddleware<T>, AddMessageProcessingMiddleware<T> | Append middleware around outgoing sends/publishes and incoming consume respectively. Middleware composes via await next() and unwinds in reverse registration order. |
InsertSendMessageMiddlewareOutermost<T>, InsertMessageProcessingMiddlewareOutermost<T> | Insert middleware at position 0 (outermost) of the matching pipeline. Use for cross-cutting concerns that must bracket every other layer (tracing, metrics). De-duplicates by middleware type — a repeat call with the same T is a no-op rather than producing two registrations, so builder.AddTelemetry() is safe to call from two feature modules. |
Minimal: RabbitMQ transport only
Section titled “Minimal: RabbitMQ transport only”services.AddServiceConnect(b => b.UseRabbitMQ(t => t.Host = "localhost"));This is enough for a producer-only service: it registers IBus, the RabbitMQ producer and consumer, and the hosted services. Handlers discovered in the app-domain assemblies are registered as transient. With no persistence registered, process-manager timeouts are unavailable; leave IBusConfiguration.EnableProcessManagerTimeouts off.
With MongoDB-backed persistence for process managers and timeouts
Section titled “With MongoDB-backed persistence for process managers and timeouts”services.AddServiceConnect(b =>{ b.UseRabbitMQ(t => { t.Host = "rabbitmq.internal"; t.Username = "shipping-service"; t.Password = "…"; });
b.UseMongoDbPersistence(opts => { opts.ConnectionString = "mongodb://mongo.internal:27017"; opts.DatabaseName = "shipping"; });
b.ConfigureQueues(q => q.QueueName = "shipping-service");
b.ConfigureBus(bus => { bus.EnableProcessManagerTimeouts = true; bus.ProcessManagerTimeoutPollInterval = TimeSpan.FromSeconds(5); });});UseMongoDbPersistence registers IProcessManagerFinder, IAggregatorPersistor, and ITimeoutStore against a single shared IMongoClient. Combined with EnableProcessManagerTimeouts, this wires up the full ShippingSaga story: state persisted in Mongo, timeouts polled from Mongo, and deliveries round-tripped through RabbitMQ back to the saga’s queue.
See also
Section titled “See also”- Getting Started — concept
- The Bus — concept
IBusConfiguration— related referenceIBus— related reference