I have a problem to resolve a Service that is registered at the .Net Core IOC.
I would like to use MediatR within my SS Services. MediatR gets registered within the Startup class.
For testing I let inject a IMediatR instance into the Configure Methods. That instance can be resolved without issues. But while creating a SS Service which has a dependency (constructor level) on IMediatR I get an exception.
So the code looks like this:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
..
services.AddMediatR(typeof(Startup));
...
}
public void Configure(... , IMediator mediator)
{
// This is working fine: mediator is not null.
if (mediator == null) throw new ArgumentNullException(nameof(mediator));
...
}
}
And my SS Service like this:
public class MyService : Service
{
private readonly IMediator _mediator;
public MyService(IMediator mediator)
{
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
}
}
I get this exception:
[14:26:22 ERR] Error occured while Processing Request: Error trying to resolve Service 'Fbi.WebApp.Services.MyService' or one of its autowired dependencies (see inner exception for details). System.Exception: Error trying to resolve Service 'Fbi.WebApp.Services.MyService' or one of its autowired dependencies (see inner exception for details). ---> System.Exception: Error trying to resolve Service 'MediatR.IMediator' or one of its autowired dependencies (see inner exception for details). ---> System.InvalidOperationException: Cannot resolve scoped service 'MediatR.IMediator' from root provider. at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator.ValidateResolution(Type serviceType, ServiceProvider serviceProvider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Funq.Container.ResolveImpl[TService](String name, Boolean throwIfMissing) in C:\BuildAgent\work\799c742886e82e6\src\ServiceStack\Funq\Container.cs:line 194 --- End of inner exception stack trace --- at Funq.Container.ResolveImpl[TService](String name, Boolean throwIfMissing) in C:\BuildAgent\work\799c742886e82e6\src\ServiceStack\Funq\Container.cs:line 199 at lambda_method(Closure , Container ) at Funq.Container.ResolveImpl[TService](String name, Boolean throwIfMissing) in C:\BuildAgent\work\799c742886e82e6\src\ServiceStack\Funq\Container.cs:line 194 --- End of inner exception stack trace --- at Funq.Container.ResolveImpl[TService](String name, Boolean throwIfMissing) in C:\BuildAgent\work\799c742886e82e6\src\ServiceStack\Funq\Container.cs:line 199 at lambda_method(Closure , IResolver ) at ServiceStack.Host.ContainerResolveCache.CreateInstance(IResolver resolver, Type type, Boolean tryResolve) in C:\BuildAgent\work\799c742886e82e6\src\ServiceStack\Host\ContainerResolveCache.cs:line 58 at ServiceStack.Host.ServiceController.<>c__DisplayClass36_0.<RegisterServiceExecutor>b__0(IRequest req, Object dto) in C:\BuildAgent\work\799c742886e82e6\src\ServiceStack\Host\ServiceController.cs:line 367 at ServiceStack.Host.ServiceController.Execute(Object requestDto, IRequest req) in C:\BuildAgent\work\799c742886e82e6\src\ServiceStack\Host\ServiceController.cs:line 536 at ServiceStack.HostContext.ExecuteService(Object request, IRequest httpReq) in C:\BuildAgent\work\799c742886e82e6\src\ServiceStack\HostContext.cs:line 144 at ServiceStack.Host.Handlers.GenericHandler.<>c__DisplayClass14_1.<ProcessRequestAsync>b__0(Task t) in C:\BuildAgent\work\799c742886e82e6\src\ServiceStack\Host\Handlers\GenericHandler.cs:line 92 at ServiceStack.AsyncExtensions.Continue[TOut](Task task, Func`2 next) in C:\BuildAgent\work\799c742886e82e6\src\ServiceStack\AsyncExtensions.cs:line 20
Looks like that SS is trying to resolve an instance by using the MS DependencyInjection Service. Any ideas?