simpleinjector / SimpleInjector.Integration.AspNetCore

MIT License
2 stars 3 forks source link

Make addition of .AddSimpleInjector's IHostedService conditional #15

Closed dotnetjunkie closed 3 years ago

dotnetjunkie commented 3 years ago

ASP.NET Core 3's new Host class resolves hosted services much earlier in the pipeline. This forced the .AddSimpleInjector extension method to add an IHostedService that ensured that the IServiceProvider was assigned to Simple Injector before such resolve takes place. This ensured that that hosted service can be injected with cross-wired dependencies.

Unfortunately, Azure Functions do not support hosted services and will throw an exception when any IHostedService is registered.

This means the addition of this hosted service by .AddSimpleInjector must be made conditional, such that Simple Injector's service collection integration can be used more easily with Azure functions.

Proposal:

services.AddSimpleInjector(this.container, options =>
{
    options.EnableHostedServiceResolution = false; // Not supported by Azure Functions.

    ...
});
dotnetjunkie commented 3 years ago

feature-15 branch added.