You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: nservicebus/hosting/extensions-hosting.md
+14-16Lines changed: 14 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,55 +2,55 @@
2
2
title: NServiceBus.Extensions.Hosting
3
3
summary: NServiceBus integration with Microsoft.Extensions.Hosting
4
4
component: Extensions.Hosting
5
-
reviewed: 2023-11-23
5
+
reviewed: 2025-08-19
6
6
related:
7
7
- samples/hosting/generic-host
8
8
- samples/dependency-injection/aspnetcore
9
9
---
10
10
11
11
## Configuration
12
12
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:
14
14
15
15
snippet: extensions-host-configuration
16
16
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.
18
18
19
19
> [!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
+
>
21
22
> > 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.
22
23
23
24
## Logging integration
24
25
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).
26
27
27
28
> [!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`.
29
30
30
31
## Dependency injection integration
31
32
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`.
33
34
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.
35
36
36
37
### Configure custom containers
37
38
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 container’s documentation for details.
39
40
40
41
partial: shutdown-timeout
41
42
42
43
## Stopping the endpoint
43
44
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.
45
46
46
47
## Installers
47
48
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.
49
50
50
-
Instead of invoking `host.`
51
+
Instead, run installers explicitly in a dedicated “setup” mode:
0 commit comments