Skip to content

Core-hosting with AddNServiceBusEndpoint#8206

Open
mattmercurio wants to merge 10 commits intomasterfrom
docs-addnservicebusendpoint-multi
Open

Core-hosting with AddNServiceBusEndpoint#8206
mattmercurio wants to merge 10 commits intomasterfrom
docs-addnservicebusendpoint-multi

Conversation

@mattmercurio
Copy link
Copy Markdown
Contributor

@mattmercurio mattmercurio commented Apr 24, 2026

No description provided.

@mattmercurio mattmercurio self-assigned this Apr 24, 2026
@mattmercurio mattmercurio requested a review from DavidBoike April 24, 2026 22:53
@mattmercurio mattmercurio force-pushed the docs-addnservicebusendpoint-multi branch from 7faf837 to 547cd44 Compare April 27, 2026 15:40
@mattmercurio mattmercurio force-pushed the docs-addnservicebusendpoint-multi branch from 547cd44 to c36de1a Compare April 27, 2026 17:41
Comment thread nservicebus/hosting/core-hosting.md
@mattmercurio mattmercurio marked this pull request as ready for review April 28, 2026 00:52
Comment thread Snippets/Core/Core_10/Hosting/AddNServiceBusEndpoint.cs Outdated
Comment thread nservicebus/hosting/core-hosting.md
Comment thread nservicebus/hosting/core-hosting.md
Comment thread nservicebus/hosting/core-hosting.md Outdated
Comment thread nservicebus/hosting/core-hosting.md Outdated
Comment thread nservicebus/hosting/core-hosting.md Outdated
Comment thread nservicebus/hosting/core-hosting.md Outdated
@danielmarbach
Copy link
Copy Markdown
Contributor

This is already pretty good!

mattmercurio and others added 4 commits April 28, 2026 14:11
Co-authored-by: Daniel Marbach <daniel.marbach@openplace.net>
Co-authored-by: Daniel Marbach <daniel.marbach@openplace.net>
Co-authored-by: David Boike <david.boike@gmail.com>
{
async Task SingleEndpoint()
{
#region AddNServiceBusEndpointSingle
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be helpful for each scenario to show an example on how to resolve things explicitely? For example we could show something like this for a single endpoint

Using the provider directly

serviceProvider.GetRequiredService<IMessageSession>();

injected into a class

MyComponent(IMessageSession session)

and for the other samples accordingly

serviceProvider.GetRequiredKeyedService<IMessageSession>("ChosenIdentifier");

and

MyComponent([FromKeyedServices("ChosenIdentifier")] IMessageSession session)

and maybe even something like

// otherThing is global
MyComponent(IMyOtherThing otherThing, [FromKeyedServices("ChosenIdentifier")] IMessageSession session)

and so on until it becomes clear?

@@ -0,0 +1,80 @@
---
title: Recommended hosting model
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm struggling with what we call this. This can only be the "recommended hosting model" as long as we don't replace it, then what would it be called? But "Microsoft Generic Host" is a dead term, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DavidBoike Yea, I've gone back and forth on this title a few times. What about "Hosting with Microsoft.Extensions.Hosting" ? Technically right but not "hey you should favor this"

- samples/hosting/generic-host
---

Starting in NServiceBus version 10.2, the recommended way to host NServiceBus is through `Microsoft.Extensions.Hosting` by registering endpoints with `IServiceCollection` using `AddNServiceBusEndpoint`. This aligns endpoint startup, dependency injection, and logging with the standard .NET hosting model, and it supports both the common single-endpoint case and advanced scenarios that host multiple endpoints in one process.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be more accurate to say something like 10.2 takes a dependency on Microsoft.Extensions.Hosting and offers hosting capabilities without requiring any other NServiceBus packages?

BUT

Remember that 6 months after 10.2 is released, 10.1 is no longer supported, and this page is V10 and up only. We should be thinking about this documentation through the lens of what it will look like to someone building an endpoint after NServiceBus 11 is released, and less like an upgrade guide.


Starting in NServiceBus version 10.2, the recommended way to host NServiceBus is through `Microsoft.Extensions.Hosting` by registering endpoints with `IServiceCollection` using `AddNServiceBusEndpoint`. This aligns endpoint startup, dependency injection, and logging with the standard .NET hosting model, and it supports both the common single-endpoint case and advanced scenarios that host multiple endpoints in one process.

## Choosing an integration path
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the previous comment, this should not be presented as "yet another choice you have to make" to use NServiceBus. We need to consider how people reading this when V11 is out and presenting it as "This is the way".

If we want to put in a partial that shows a note box about the old way in V10 only, that's possible though.


## Hosting a single endpoint

Register the endpoint on the host's service collection. For new development, this is the recommended alternative to the `UseNServiceBus` pattern in `NServiceBus.Extensions.Hosting`.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't compare to UseNServiceBus just tell how to do it


snippet: AddNServiceBusEndpointSingle

The endpoint starts and stops with the host's lifecycle. `IMessageSession` is available in dependency injection for components that need to send or publish from outside a message handler.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use slightly more precise language about what happens with the IMessageSession?


## Endpoint identity and the DI identifier

- **Endpoint name** — the NServiceBus identity set on `EndpointConfiguration`. Drives the input queue, routing, diagnostics, and log context. Unique across the system.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unique across the system

I don't think that's true?

Also, either we can write complete sentences, or if we're really trying to be terse, we can put all of this in a comparison table.


In a single-endpoint host, the two are the same value and the distinction does not matter. In a multi-endpoint host, each endpoint retains its own input queue, `IMessageSession`, and per-endpoint configuration.

## Logging
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could see a sample of how log output looks with multiple endpoints, but most of the stuff below this line is upgrade guide material.

### Stop the endpoint

[Microsoft Generic Host's](/nservicebus/hosting/extensions-hosting.md) `IHostApplicationLifetime.Stop` method stops the NServiceBus endpoint gracefully.
When hosted with [`Microsoft.Extensions.Hosting`](https://learn.microsoft.com/en-us/dotnet/core/extensions/generic-host), calling `IHostApplicationLifetime.StopApplication()` stops the host gracefully, which in turn stops the NServiceBus endpoint.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if "when hosting with package I never added to the project myself" is going to land right?

Comment on lines +21 to +22
- [Recommended hosting model](/nservicebus/hosting/core-hosting.md) using the built-in `AddNServiceBusEndpoint` integration.
- [NServiceBus.Extensions.Hosting](/nservicebus/hosting/extensions-hosting.md) for the existing package-based `UseNServiceBus` integration.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be turned into partials, because in V9 and below there is only option and in V10 and above (discounting 10.0 and 10.1 which ultimately go away) there is only one (albeit different) choice.

### Using Microsoft.Extensions.Hosting

NServiceBus can be integrated into any web host that supports the [Microsoft Generic Host](https://docs.microsoft.com/en-us/dotnet/core/extensions/generic-host) using the [NServiceBus Generic Host integration](/nservicebus/hosting/extensions-hosting.md). The Generic Host integration automatically registers `IMessageSession` with the dependency injection container.
NServiceBus integrates with any web host that supports the [.NET Generic Host](https://docs.microsoft.com/en-us/dotnet/core/extensions/generic-host). For new development, use the [recommended hosting model](/nservicebus/hosting/core-hosting.md). Existing applications can continue with [NServiceBus.Extensions.Hosting](/nservicebus/hosting/extensions-hosting.md). In both paths, `IMessageSession` is available through dependency injection.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here about partials.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants