﻿# ASP.NET Core Dependency Injection Integration


[ASP.NET Core](https://learn.microsoft.com/en-us/aspnet/core/) has an integrated [dependency injection (DI) feature](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection). When hosting NServiceBus endpoints inside an ASP.NET Core app, sharing components registered for DI between ASP.NET components and NServiceBus message handlers may be necessary. Use the [NServiceBus.Extensions.Hosting package](https://www.nuget.org/packages/NServiceBus.Extensions.Hosting) to host an endpoint as part of an ASP.NET Core application.

### Configuring an endpoint to use built-in DI

<!-- snippet: ContainerConfiguration -->

```cs
builder.Services.AddSingleton<MyService>();

var endpointConfiguration = new EndpointConfiguration("Sample.Core");
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
endpointConfiguration.UseTransport<LearningTransport>();

builder.Services.AddNServiceBusEndpoint(endpointConfiguration);
```

<!-- endsnippet -->

When the sample is run, a web application is started. All web requests received will trigger a message send operation:

<!-- snippet: RequestHandling -->

```cs
app.MapGet("/", context =>
{
    var endpointInstance = context.RequestServices.GetService<IMessageSession>();
    var myMessage = new MyMessage();

    return Task.WhenAll(
        endpointInstance.SendLocal(myMessage),
        context.Response.WriteAsync("Message sent"));
});
```

<!-- endsnippet -->

Message handlers will have dependencies injected at runtime by the configured Inversion of Control (IoC) container:

<!-- snippet: InjectingDependency -->

```cs
public class MyHandler(MyService myService) : IHandleMessages<MyMessage>
{
    public Task Handle(MyMessage message, IMessageHandlerContext context)
    {
        myService.WriteHello();
        return Task.CompletedTask;
    }
}
```

<!-- endsnippet -->

### Configuring to use shared DI with Autofac

It is also possible to configure ASP.NET Core to use a specific container and still share components between ASP.NET and NServiceBus message handlers. This sample demonstrates how to do this with Autofac using the `Autofac.Extensions.DependencyInjection` package

> [!NOTE]
> This can also be done using other [DI libraries](/nservicebus/dependency-injection/index.md).

First, ASP.NET Core is instructed to use a custom container:

<!-- snippet: ServiceProviderFactoryAutofac -->

```cs
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
```

<!-- endsnippet -->

Then, `Startup` can use Autofac natively to configure services:

<!-- snippet: ContainerConfigurationAutofac -->

```cs
builder.Host.ConfigureContainer<ContainerBuilder>(containerBuilder => containerBuilder.RegisterType<MyService>().SingleInstance());
```

<!-- endsnippet -->

The [`ConfigureServices` method](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/startup#the-configureservices-method) is called by .NET Core at [application startup time](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/startup) to register additional services. The `ConfigureContainer` method registers the components in the container using the container's native APIs.
