﻿# Hosting

<!-- Version variant: core_8; default: [/nservicebus/hosting/index.md](/nservicebus/hosting/index.md) -->


NServiceBus is a library at its core so that it can be hosted in any .NET process.

There are several approaches to hosting.

## Microsoft Generic Host

The [Microsoft Generic Host](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host) is the most common way to host NServiceBus on .NET Core. NServiceBus integrates with the generic host using the [NServiceBus.Extensions.Hosting package](/nservicebus/hosting/extensions-hosting.md).

## Self-hosting

"Self-hosting" is a general term used when the application code takes full control over all facets of hosting NServiceBus. This includes the following actions:

 * Configuration
 * [Logging](/nservicebus/logging/index.md)
 * [Dependency injection](/nservicebus/dependency-injection/index.md)
 * [Endpoint Lifecycle](/nservicebus/lifecycle/index.md)
 * [Critical Error handling](critical-errors.md)

> [!NOTE]
> It is recommended that the default critical error callback be overridden when self-hosting NServiceBus. Refer to the [Critical Errors](/nservicebus/hosting/critical-errors.md) article for more information.

When self-hosting, the user is responsible for creating and starting the endpoint instance:

<!-- snippet: Hosting-Startup -->

```cs
var endpointConfiguration = new EndpointConfiguration("EndpointName");
// Apply other necessary endpoint configuration, e.g. transport
var startableEndpoint = await Endpoint.Create(endpointConfiguration);
var endpointInstance = await startableEndpoint.Start();

// Shortcut
var endpointInstance2 = await Endpoint.Start(endpointConfiguration);
```

<!-- endsnippet -->

The user is also responsible for properly shutting down the endpoint when it is no longer needed (usually when the application terminates).

<!-- snippet: Hosting-Shutdown -->

```cs
await endpointInstance.Stop();
```

<!-- endsnippet -->

> [!NOTE]
> The endpoint instance is not disposable due to the asynchronous nature of the pipeline. Call `Stop` in an async manner (see example above).

### Windows Service hosting

A [Windows Service](https://learn.microsoft.com/en-us/dotnet/framework/windows-services/introduction-to-windows-service-applications) is a common way to host NServiceBus in Windows.

Related:
 * [Generic host as Windows Service](/samples/hosting/generic-host/index.md)
 * [Generate a Windows Service project using `dotnet new`](/nservicebus/dotnet-templates/index.md)
 * [Windows Service installation](windows-service.md)

### Docker container hosting

An endpoint can be hosted inside a [Docker](https://www.docker.com/) container.

Related:
 * [Docker container host](/nservicebus/hosting/docker-host/index.md)
 * [Generate a Docker-hosted endpoint project with `dotnet new`](/nservicebus/dotnet-templates/index.md)
 * [Hosting endpoints in Docker Linux containers](/samples/hosting/docker/index.md)
 * [Generic host](/samples/hosting/generic-host/index.md)

## Hosting technologies

### Web hosting

NServiceBus can be hosted using any web technology that supports .NET. See [Web Application Hosting](web-application.md) for more information.

### WebJob hosting

NServiceBus can be hosted in a WebJob. See [Self-Hosting in Azure WebJobs](/samples/azure/webjob-host/index.md)

### Serverless hosting

NServiceBus can be hosted in several serverless environments such as [Azure Functions](/nservicebus/hosting/azure-functions-service-bus/index.md) and [AWS Lambda](/nservicebus/hosting/aws-lambda-simple-queue-service/index.md).

## Hosting environment requirements

NServiceBus endpoints have certain requirements for the hosting environment:

* The endpoint process needs write access to write log files. See the [logging documentation](/nservicebus/logging/index.md) for more details about the default log file location and how to configure logging.
* The endpoint process needs write access to write the startup diagnostics file. See the [startup diagnostics documentation](/nservicebus/hosting/startup-diagnostics.md) for more details about the diagnostic file.
* Since NServiceBus makes assumptions on aspects like assembly names, ILMerging any of the NServiceBus* assemblies is not supported.
