Skip to content

Commit 76ad4b9

Browse files
authored
Review hosting extension
1 parent 7674219 commit 76ad4b9

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

nservicebus/hosting/extensions-hosting.md

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,55 @@
22
title: NServiceBus.Extensions.Hosting
33
summary: NServiceBus integration with Microsoft.Extensions.Hosting
44
component: Extensions.Hosting
5-
reviewed: 2023-11-23
5+
reviewed: 2025-08-19
66
related:
77
- samples/hosting/generic-host
88
- samples/dependency-injection/aspnetcore
99
---
1010

1111
## Configuration
1212

13-
An NServiceBus endpoint can be hosted within the generic host with the `UseNServiceBus` extension method:
13+
An NServiceBus endpoint can be hosted in the .NET Generic Host using the `UseNServiceBus` extension method:
1414

1515
snippet: extensions-host-configuration
1616

17-
This code will register the endpoint with the hosting infrastructure and automatically start and stop it based on the host's application lifetime.
17+
This registers the endpoint with the hosting infrastructure and starts/stops it automatically with the application's lifetime.
1818

1919
> [!WARNING]
20-
> Specify `UseNServiceBus` before any other service (e.g. `ConfigureWebHostDefaults`) which requires access to the `IMessageSession`. Incorrect usage results in a `System.InvalidOperationException` with the following message:
20+
> Call `UseNServiceBus` **before** registering any component that needs `IMessageSession`. Placing it later can cause a `System.InvalidOperationException`:
21+
>
2122
> > The message session can't be used before NServiceBus is started. Place `UseNServiceBus()` on the host builder before registering any hosted service (e.g. `services.AddHostedService<HostedServiceAccessingTheSession>()`) or the web host configuration (e.g. `builder.ConfigureWebHostDefaults`) if hosted services or controllers require access to the session.
2223
2324
## Logging integration
2425

25-
NServiceBus logging is automatically configured to use the [logging configured](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging) for the [generic host](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host); no NServiceBus specific logging configuration is needed.
26+
NServiceBus logging is automatically wired to the host's logging pipeline. No NServiceBus-specific logging configuration is required. See [.NET logging](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging) and the [Generic Host](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host).
2627

2728
> [!WARNING]
28-
> [NServiceBus.Extensions.Logging](/nservicebus/logging/extensions-logging.md) or [NServiceBus.MicrosoftLogging.Hosting](https://www.nuget.org/packages/NServiceBus.MicrosoftLogging.Hosting) should not be used.
29+
> Do **not** use [NServiceBus.Extensions.Logging](/nservicebus/logging/extensions-logging.md) or [NServiceBus.MicrosoftLogging.Hosting](https://www.nuget.org/packages/NServiceBus.MicrosoftLogging.Hosting) with `NServiceBus.Extensions.Hosting`.
2930
3031
## Dependency injection integration
3132

32-
NServiceBus endpoints hosted as part of the generic host automatically use the provided `IServiceCollection` and `IServiceProvider` dependency injection infrastructure. Message handlers can resolve dependencies which are registered in the `IServiceCollection`.
33+
When hosted in the Generic Host, NServiceBus uses the application's `IServiceCollection` / `IServiceProvider`. Message handlers can resolve services registered in `IServiceCollection`.
3334

34-
`UseNServiceBus` automatically registers an `IMessageSession` with the container which can be resolved from the `IServiceProvider` or via dependency injection during runtime.
35+
`UseNServiceBus` also registers an `IMessageSession` that can be resolved from the container or injected where needed at runtime.
3536

36-
### Configure custom containers
37+
### Using a custom DI container
3738

38-
Custom dependency injection containers may be configured using `IWebHostBuilder.UseServiceProviderFactory`. NServiceBus automatically uses the host's dependency injection container. Refer to the container's documentation for further details.
39+
To use a third-party container, configure it with `HostBuilder.UseServiceProviderFactory(...)`. NServiceBus will automatically use the host’s container. Refer to your containers documentation for details.
3940

4041
partial: shutdown-timeout
4142

4243
## Stopping the endpoint
4344

44-
When using the generic host, the `IEndpointInstance` interface to stop the endpoint is not directly exposed. To stop the endpoint, use the [`IApplicationLifetime`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.ihostapplicationlifetime) interface to gracefully stop the NServiceBus endpoint and other hosted services. See the [generic host application lifetime documentation](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host#ihostapplicationlifetime) for further information.
45+
With the Generic Host, the `IEndpointInstance` used to stop the endpoint is not exposed directly. To shut down gracefully, request application shutdown via [`IHostApplicationLifetime`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.ihostapplicationlifetime). See [Generic Host application lifetime](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host#ihostapplicationlifetime) for more information.
4546

4647
## Installers
4748

48-
It is not recommended to always run the [NServiceBus installers](/nservicebus/operations/installers.md) by invoking `.EnableInstallers()` as this delays startup time and might require more permissions.
49+
Avoid always running [NServiceBus installers](/nservicebus/operations/installers.md) via `.EnableInstallers()` as it adds startup time and may require elevated permissions.
4950

50-
Instead of invoking `host.`
51+
Instead, run installers explicitly in a dedicated “setup” mode:
5152

52-
Example:
53-
```c#
53+
```csharp
5454
var isSetup = args.Contains("-s") || args.Contains("/s");
5555

5656
if (isSetup)
@@ -65,5 +65,3 @@ if (isSetup)
6565

6666
// Continue and eventually invoke:
6767
// await host.RunAsync();
68-
```
69-

0 commit comments

Comments
 (0)