Create REST Subscriber¶
This page shows how to create the RabbitMQ REST host subscriber manually.
The concrete implementation is:
RestHostSubscriberBound
Unlike the core publisher and subscriber implementations, RestHostSubscriberBound is provided by the ASP.NET Core integration package.
Install The ASP.NET Core Package¶
Install the ASP.NET Core integration package:
dotnet add package ServiceBus.AspNetCore.Rabbit
This package contains the components required to host ASP.NET Core endpoints over RabbitMQ, including:
RestHostSubscriberBoundIRestHostSubscriber- ASP.NET Core integration services
Required Dependencies¶
RestHostSubscriberBound requires:
RestSubscriberBoundDescriptor- RabbitMQ channel
IDataFormatterIHttpRestMessageSerializerIMemoryStreamResolverIHttpContextFactoryIServiceScopeFactoryILogger<RestHostSubscriberBound>
This guide assumes that the required formatter has already been created.
See:
- Getting Started → Create Formatter
IDataFormatter formatter = ...;
Manual creation is useful for understanding the object model, testing or building custom hosting scenarios.
For production applications, dependency injection is the recommended approach.
Create Descriptor¶
var descriptor =
new RestSubscriberBoundDescriptor
{
ServiceName = "pricing",
Durable = true,
PrefetchCount = 10
};
ServiceName identifies the exchange and queue used by the REST host subscriber.
Create Connection And Channel¶
var factory = new ConnectionFactory
{
HostName = "localhost",
UserName = "guest",
Password = "guest",
DispatchConsumersAsync = true
};
using var connection =
factory.CreateConnection("rest-subscriber-demo");
using var channel =
connection.CreateChannel();
Create REST Message Serializer¶
var streamResolver =
new FuncMemoryStreamResolver(
() => new MemoryStream(),
bytes => new MemoryStream(bytes));
IHttpRestMessageSerializer messageSerializer =
new HttpRestMessageSerializer(
streamResolver,
Encoding.UTF8);
Resolve ASP.NET Core Services¶
The remaining dependencies are provided by the ASP.NET Core service container.
var httpContextFactory =
serviceProvider.GetRequiredService<IHttpContextFactory>();
var serviceScopeFactory =
serviceProvider.GetRequiredService<IServiceScopeFactory>();
var logger =
serviceProvider
.GetRequiredService<ILogger<RestHostSubscriberBound>>();
Create Subscriber¶
await using var subscriber =
new RestHostSubscriberBound(
descriptor,
channel,
formatter,
messageSerializer,
streamResolver,
httpContextFactory,
serviceScopeFactory,
logger);
Initialize The Request Pipeline¶
RestHostSubscriberBound executes the ASP.NET Core request pipeline for every incoming RabbitMQ request.
The Init() method expects an ASP.NET Core RequestDelegate.
When creating the subscriber manually, the request delegate is obtained by building the ASP.NET Core application pipeline.
RequestDelegate requestDelegate =
app.Build();
subscriber.Init(requestDelegate);
Where:
appis anIApplicationBuilder;Build()compiles the configured middleware pipeline into aRequestDelegate;- the resulting
RequestDelegateis executed for every incoming RabbitMQ request.
Init() must be called before Start().
Start Subscriber Manually¶
await subscriber.Start();
Stop Subscriber Manually¶
await subscriber.Stop();
Starting Through Dependency Injection¶
When using ASP.NET Core dependency injection, manual initialization is not required.
The extension method:
app.UseSubscriberHost();
performs the complete initialization sequence.
Internally, it:
- resolves the registered
IRestHostSubscriber; - builds the ASP.NET Core request pipeline using
app.Build(); - passes the resulting
RequestDelegatetoInit(); - starts the subscriber when the application starts;
- stops the subscriber gracefully during application shutdown.
Conceptually, the extension performs the following operations:
var host =
serviceProvider.GetRequiredService<IRestHostSubscriber>();
host.Init(app.Build());
await host.Start();
and, during application shutdown:
await host.Stop();
This approach is recommended for production applications because it integrates the subscriber with the ASP.NET Core hosting lifecycle.
Runtime Flow¶
sequenceDiagram
participant RabbitMQ
participant Subscriber as RestHostSubscriberBound
participant Pipeline as RequestDelegate
participant ASPNET as ASP.NET Core Pipeline
RabbitMQ->>Subscriber: Request message
Subscriber->>Pipeline: Invoke()
Pipeline->>ASPNET: Execute middleware
ASPNET-->>Pipeline: HTTP response
Pipeline-->>Subscriber: Response
Subscriber-->>RabbitMQ: Response message
Related Documentation¶
See also:
- ASP.NET Core Integration → Overview
- ASP.NET Core Integration → RestHostSubscriberBound
- ASP.NET Core Integration → Example
- Getting Started → Create Formatter