NServiceBus easily integrates with ASP.NET Core applications.
Minimal APIs
ASP.NET 6 introduced a new hosting API with the ASP.NET minimal APIs. The NServiceBus.Extensions.Hosting package is fully compatible with the minimal API's WebApplication host:
var builder = WebApplication.CreateBuilder();
var endpointConfiguration = new EndpointConfiguration("MyWebAppEndpoint");
// configure the endpoint
builder.UseNServiceBus(endpointConfiguration);
var app = builder.Build();
// further ASP.NET configuration
app.Run();
Generic Host
Starting with ASP.NET 3, the NServiceBus Generic Host support provides integration with ASP.NET Core applications:
var builder = WebApplication.CreateBuilder();
var endpointConfiguration = new EndpointConfiguration("MyWebAppEndpoint");
// configure the endpoint
builder.UseNServiceBus(endpointConfiguration);
var app = builder.Build();
// Further ASP.NET configuration
app.Run();
Reading application settings
NServiceBus is configured in code. Values such as endpoint names, connection strings, and feature flags can be sourced from appsettings. or any other configuration provider by reading them via IConfiguration and passing them to the NServiceBus configuration API.
When using the Minimal API host, read configuration via builder.:
var builder = WebApplication.CreateBuilder();
var endpointName = builder.Configuration.GetValue<string>("NServiceBus:EndpointName")
?? "MyWebAppEndpoint";
var endpointConfiguration = new EndpointConfiguration(endpointName);
// configure endpoint, passing values from builder.Configuration as needed
builder.UseNServiceBus(endpointConfiguration);
var app = builder.Build();
// further ASP.NET configuration
app.Run();
When using the Generic Host, read configuration via the HostBuilderContext (or hostBuilder.):
var hostBuilder = Host.CreateApplicationBuilder();
var endpointName = hostBuilder.Configuration.GetValue<string>("NServiceBus:EndpointName")
?? "MyEndpoint";
var endpointConfiguration = new EndpointConfiguration(endpointName);
// configure endpoint, passing values from hostBuilder.Configuration as needed
hostBuilder.UseNServiceBus(endpointConfiguration);
var host = hostBuilder.Build();
await host.RunAsync();
Both WebApplication. and Host. automatically load configuration from:
appsettings.json appsettings.(for example,{Environment}. json appsettings.)Development. json - Environment variables
No additional setup is required to enable these sources. See Reading application settings for guidance on connection strings, the options pattern, and other configuration providers.
Dependency injection
Messages can be sent with NServiceBus using the IMessageSession API, which is available via the dependency injection container.
Web API controllers
[ApiController]
class WebApiController(IMessageSession messageSession) : ControllerBase
{
[HttpGet]
public async Task<ActionResult> Get()
{
await messageSession.Send(new MessageFromWebApi());
return Ok("message was sent successfully");
}
}
MVC controllers
public class MvcController(IMessageSession messageSession) : Controller
{
public ViewResult Index()
{
return View();
}
public async Task<ViewResult> SendMessage()
{
await messageSession.Send(new MessageFromMvc());
return View();
}
}
Razor Pages
public class RazorPage(IMessageSession messageSession) : PageModel
{
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
await messageSession.Send(new MessageFromRazorPage());
return RedirectToPage("./Success");
}
}
Older ASP.NET Core versions
For web applications using ASP.NET Core 2.x, the Community.NServiceBus.WebHost community package provides integration with the legacy WebHost.