jbogard / MediatR.Extensions.Microsoft.DependencyInjection

MediatR extensions for Microsoft.Extensions.DependencyInjection
MIT License
327 stars 89 forks source link

AddRequiredServices lifetime #61

Closed ogaudefroy closed 5 years ago

ogaudefroy commented 5 years ago

Hey there,

I'm having a few issues using this library in an application where commands are received via HTTP calls but also via a messaging infrastructure.

Digging a bit in the code I found that current implementation of AddRequiredServices strongly relies on a scoped context while the acual configuration allows you to register a custom lifetime.

private static void AddRequiredServices(IServiceCollection services, MediatRServiceConfiguration serviceConfiguration)
{
   services.AddScoped<ServiceFactory>(p => p.GetService);
   services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>));
   services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPostProcessorBehavior<,>));
   services.Add(new ServiceDescriptor(typeof(IMediator), serviceConfiguration.MediatorImplementationType, serviceConfiguration.Lifetime));
}

I implemented a workaround by registering explicitely the components with another lifetime ; so my question is did I miss something or should the registered components also use the provided lifetime?

Regards, Olivier

jbogard commented 5 years ago

Things get Really Weird if you change the lifecycle, so much so I'd say don't use this extension unless you want this lifecycle that I configure. It's the "Easy Mode" unless you're trying to use MediatR outside a scope, which is by far the minority.

ogaudefroy commented 5 years ago

Thx for the answer jimmy. Have a nice day