Can't resolve Service which is registered at .Net Core IOC

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?

Can you resolve the dependency using ServiceStacks IOC in AppHost.Configure()?

No, I can’t

public override void Configure(Container container)
{
  var instance = container.Resolve<IMediator>();
  ... 
}

Throws

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 Fbi.WebApp.FbiWebAppHost.Configure(Container container) in C:\Projekte\VS2017\DLR\Sourcen\src\Fbi.WebApp\FbiWebAppHost.cs:line 42
at ServiceStack.ServiceStackHost.Init() in C:\BuildAgent\work\799c742886e82e6\src\ServiceStack\ServiceStackHost.cs:line 199
at ServiceStack.NetCoreAppHostExtensions.UseServiceStack(IApplicationBuilder app, AppHostBase appHost) in C:\BuildAgent\work\799c742886e82e6\src\ServiceStack\AppHostBase.NetCore.cs:line 217
at Fbi.WebApp.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, IMediator mediator) in C:\Projekte\VS2017\DLR\Sourcen\src\Fbi.WebApp\Startup.cs:line 55
— End of stack trace from previous location where exception was thrown —
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
at Microsoft.AspNetCore.ApplicationInsights.HostingStartup.ApplicationInsightsLoggerStartupFilter.<>c__DisplayClass0_1.b__0(IApplicationBuilder builder)
at Microsoft.ApplicationInsights.AspNetCore.ApplicationInsightsStartupFilter.<>c__DisplayClass0_0.b__0(IApplicationBuilder app)
at Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter.<>c__DisplayClass0_0.b__0(IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at Fbi.WebApp.Program.BuildWebHost(String args) in C:\Projekte\VS2017\DLR\Sourcen\src\Fbi.WebApp\Program.cs:line 43
at Fbi.WebApp.Program.Main(String args) in C:\Projekte\VS2017\DLR\Sourcen\src\Fbi.WebApp\Program.cs:line 28

Looks like the issue is that IMediator is registered as a scoped dependency that can’t be resolved from the root non-Request scoped dependency that ServiceStacks .NET Core IOC Adapter uses to resolve .NET Core dependencies.

Doesn’t look like you’ll be able to share the same IMediator registration as .NET Core’s and would need to maintain a separate registration of IMediator in AppHost.Configure()

FYI, I’ve just committed a change that now uses a new IOC scope in the NetCoreContainerAdapter which may resolve this issue. This change is available from v5 that’s now available on MyGet, please see v5 changes before upgrading.