0

I am running into an error 'Unable to resolve service for type 'MassTransit.IBusControl' while attempting to activate 'PCNDmzWeb.Api.Services.MassTransitHostedService'.'"

I am using the boilerplate code for MVC Core with a few minor changes. In the Configure services method i have the following:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    _container.RegisterInstance<IBusControl>(
        Bus.Factory.CreateUsingRabbitMq(sbc =>
        {
            var host = sbc.Host("localhost", "dev", h =>
            {
                h.Username("guest");
                h.Password("guest");
            });
            sbc.ReceiveEndpoint(host, "job_results_queue", e =>
            {
                e.Consumer<StoreJobResultsConsumer>();
            });
        }));

    services.AddScoped<IHostedService, MassTransitHostedService>();

    IntegrateSimpleInjector(services);
}

It does not appear that the instance of the BusControl is getting created. The message seems to indicate that the error is occurring in the Microsoft Extensions Dependency Injection. What am I doing wrong?

Stack Trace follows:

Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, ISet1 callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(Type serviceType, Type implementationType, ISet1 callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, ISet1 callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateEnumerable(Type serviceType, ISet1 callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, ISet1 callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, ISet1 callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(Type serviceType, Type implementationType, ISet1 callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, ISet1 callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Type serviceType, ISet1 callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, ISet1 callSiteChain) at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType, ServiceProvider serviceProvider) at System.Collections.Concurrent.ConcurrentDictionaryExtensions.GetOrAdd[TKey,TValue,TArg](ConcurrentDictionary2 dictionary, TKey key, Func3 valueFactory, TArg arg) 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 Microsoft.AspNetCore.Hosting.Internal.WebHost.d__26.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Hosting.WebHostExtensions.d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Hosting.WebHostExtensions.d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Hosting.WebHostExtensions.Run(IWebHost host) at PCNDmzWeb.Api.Program.Main(String[] args) in C:\myapp\PCNDmzWeb.Api\Program.cs:line 17

Alex Riabov
  • 8,655
  • 5
  • 47
  • 48
Koll
  • 1
  • 1
  • What is in your IntegrateSimpleInjector method? – Alex Riabov Oct 11 '18 at 15:30
  • 1
    Seems like a [double post](https://github.com/simpleinjector/SimpleInjector/issues/621). Please inform us on why the suplied answer is insufficient so anyone trying to answer this can take this into account. – Steven Oct 11 '18 at 16:24
  • Ant reason why you are using SimpleInjector instead of what is already there? – PmanAce Oct 11 '18 at 16:28
  • 1
    Steven is right, the last post in the GitHub issue you created answers your question. ASP.NET Core DI won't ask SimpleInjector for missing dependencies. Cross-wiring works only for SimpleInjector asking for services registered in the service collection of the Microsoft DI. You either need to move the bus registration to the Microsoft DI or find a way to start and stop the bus without using the IHostedService, which is not hard. – Alexey Zimarev Oct 11 '18 at 18:37
  • 1
    @PmanAce therecare many reasons for using a 3rd party DI container. The built-in container has a really limited feature set. – Steven Oct 12 '18 at 06:53
  • @Steven you could be right but I use it in production and can't imagine what I'm missing with using the built-in one. Care to enlighten me? – PmanAce Oct 12 '18 at 11:44
  • @PmanAce a short answer can be found [here](https://stackoverflow.com/questions/30681477/why-would-one-use-a-third-party-di-container-over-the-built-in-asp-net-core-di-c/30682214) and a much more elaborate explanation can be found in [this book](https://manning.com/seemann2) that describes how to build highly maintainable applications using DI and SOLID design principles. Especially chapter 10 about AOP and chapter 15 on how MS.DI lacks many features. – Steven Oct 12 '18 at 12:13
  • @Steven I have that book and read it, in our case the built-in DI for NetCore 2.1 is still pretty good. We don't need to verify the configuration as mentioned in your link. As for cross-cutting concerns, I have a couple that the DI can inject without issues. I still don't see a concrete reason in your link or mentioned by you. – PmanAce Oct 12 '18 at 13:29
  • @PmanAce if it works for you, that's great. No need to replace it. My experiemce, however, is that once you start designing your application around SOLID generic abstraction and use autp-registration and decorators, the built-in container becomes rather useless and actually in the way. I prefer Pure DI over that container. – Steven Oct 12 '18 at 13:34
  • @Steven don't get me wrong, not trying to nitpick, just trying to understand. In this case, it doesn't seem like the poster needs advanced DI over the built in one. As for the SOLID paradigm, it doesn't state anything about generic abstractions, the built-in DI container doesn't violate any letter in SOLID. As for using Pure DI instead the built-in container, nowhere do I use the service locator pattern so I'm not too sure what you are getting at. – PmanAce Oct 12 '18 at 18:12
  • The supplied answer throws an invalid operation exception : Cannot consume scoped service 'Microsoft.Extensions.Hosting.IHostedService' from singleton 'Microsoft.AspNetCore.Hosting.Internal.HostedServiceExecutor'.' – Koll Oct 12 '18 at 21:20
  • I had posted it there because I assumed this was an issue related to simpleinjector. Mass transit suggests posting on stackoverflow concering it's questions. I will continue my question on GitHub so as to avoid duplication. Thanks. – Koll Oct 12 '18 at 21:23
  • 1
    @PmanAce, no offence taken. Neither both of us can, based on the question at hand, determine whether OP requires advanced DI capabilities or not. Because we can't, and the sheer difference between 3rd party containers and the built-in one, IMO, we must assume OP requires a different container. About SOLID, since you already seem to have purchased the [second edition](https://manning.com/seemann2) (which is awesome!), please read chapter 10 and 15. It's hard for me to describe in a comment what I wrote in those 70 pages. – Steven Oct 13 '18 at 08:50

0 Answers0