simpleinjector / SimpleInjector.Integration.AspNetCore

MIT License
2 stars 3 forks source link

Register IServiceScope before calling Action<SimpleInjectorAddOptions> setupAction #17

Closed dotnetjunkie closed 3 years ago

dotnetjunkie commented 3 years ago

The .AddSimpleInjector() extension method adds the registration for the framework's IServiceScope to the Simple Injector container, but it does so after calling the setupAction. This is unfortunate, because this makes it really hard for any extension on top of SimpleInjectorAddOptions that wants to replace the IServiceScope to do so.

Use case is Blazor integration. A hypothetical integration would look like this:

services.AddSimpleInjector(container, options =>
{
    options.AddServerSideBlazor();
});

This integration, however, needs to replace the IServiceScope, but at the time AddServerSideBlazor is called, IServiceScope hasn't been registered, as AddSimpleInjector adds this registration after the delegate call. This causes that last registration to fail, because at that point there already exists a registration for IServiceScope.

By moving this registration before calling the delegate, the AddServerSideBlazor can easily replace that registration.

Do note that this change is theoretically a breaking change, although the chance that any user is impacted by this is pretty low.

dotnetjunkie commented 3 years ago

feature-17 branch created.