Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in BusClientFactory.CreateDefault() on net core 2.0 #257

Closed
Kilovatiy opened this issue Aug 17, 2017 · 14 comments
Closed

Error in BusClientFactory.CreateDefault() on net core 2.0 #257

Kilovatiy opened this issue Aug 17, 2017 · 14 comments

Comments

@Kilovatiy
Copy link

Kilovatiy commented Aug 17, 2017

Hi,
I use RawRabbit.vNext version 1.10.3.
I update my app from core 1.2 to core 2.0 and start receiving an error -
Method not found:'System.IServiceProvider Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(Microsoft.Extensions.DependencyInjection.IServiceCollection)'.

Here is stack trace:
at RawRabbit.vNext.BusClientFactory.CreateDefault(IServiceCollection services) at IBE.Amadeus.SessionPool.MessageQueueClientFactory.GetSingletonInstance(RawRabbitConfiguration config) in C:\Work_proj\ibe-backend\IBE.Amadeus.SessionPool\MessageQueueClientFactory.cs:line 27 at IBE.Amadeus.SessionPool.ServiceCollectionExtensions.<>c__DisplayClass0_0.<AddSessionPool>b__0(IServiceProvider ioc) in C:\Work_proj\ibe-backend\IBE.Amadeus.SessionPool\ServiceCollectionExtensions.cs:line 34 at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingleton(SingletonCallSite singletonCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(IServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass22_0.<RealizeService>b__0(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType) at Microsoft.Extensions.Internal.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired) at lambda_method(Closure , IServiceProvider , Object[] ) at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass4_0.<CreateActivator>b__0(ControllerContext controllerContext) at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController0(ControllerContext controllerContext) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeInnerFilterAsync>d__14.MoveNext()

Thanks

@pardahlman
Copy link
Owner

Hi, @Kilovatiy - thanks for reporting this. I haven't had time to look into the changes of .NET Core 2.0, but apparently some contracts have changed. I'm focusing on v 2.0 of RawRabbit ATM, but feel free to submit a PR for this!

@pardahlman
Copy link
Owner

Not sure what the underlying problem is. Based on this dotnet/efcore#8498 (comment) it sounds like the problem can be resolved by removing the bin folder.

I looked into the possibility to upgrade Microsoft.Extensions.DependencyInjection to version 2.0.0, but it requires .NET Standard 2.0 which is a higher version than currently supported.

@samirski
Copy link

samirski commented Aug 23, 2017

I think this has something to do with the netcoreapp assembly bindings. Strangely enough this exception is not being thrown in a .net core console application but only in a .net core web application for whatever reason. So I took a look in the BusClientFactory class and tried to construct the objects the same way in my code. Long story short everything works now as expected. It might be a bit cumbersome but frankly speaking I don't care as the bus client object can be stored in a IoC container as well. So my code looks like this now:

var services = new ServiceCollection().AddRawRabbit( cfg => cfg.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("rawrabbit.json"), ioc => ioc.AddSingleton<RawRabbit.Logging.ILoggerFactory, RawRabbit.Logging.Serilog.LoggerFactory>());

var serviceProvider = services.BuildServiceProvider(); var client = serviceProvider.GetService<IBusClient>(); var busClient = new RawRabbit.vNext.Disposable.BusClient(client);

and similarly while using the AdvancedMessageContext:

var services = new ServiceCollection().AddRawRabbit<AdvancedMessageContext>( cfg => cfg.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("rawrabbit.json"), ioc => ioc.AddSingleton<RawRabbit.Logging.ILoggerFactory, RawRabbit.Logging.Serilog.LoggerFactory>());

var serviceProvider = services.BuildServiceProvider(); var client = serviceProvider.GetService<IBusClient<AdvancedMessageContext>>(); var busClient = new RawRabbit.vNext.Disposable.BusClient<AdvancedMessageContext>(client);

BTW, don't forget to reference Microsoft.Extensions.DependencyInjection 2.0 as I was still getting an error without it.

@glenthomas
Copy link

I have the same problem. RawRabbit in .NET Core 2.0 console application was fine, .NET Core 2.0 web hosted application is not working, with above error.

@samirski I don't understand your solution. You are adding RawRabbit to DI service provider, but then you are instantiating the bus client without using the service provider..? Why not just skip adding it to the service collection?

@glenthomas
Copy link

It looks like an upgrade to .NET Standard 2.0 is the only option. @pardahlman If I submitted a PR for an upgrade to 2.0 would you be ok with that or looking to keep compatibility with lower version?

Will RawRabbit v2 target .NET Standard 2.0?

@glenthomas
Copy link

Can work around this by resolving RawRabbit.IBusClient instead of RawRabbit.vNext.Disposable.IBusClient

@pardahlman
Copy link
Owner

@MrGlenThomas I'm hesitant to target .NET Standard 2.0, as it will break compatibility with users that target a framework compatible with a lower standard (for example .NET Framework 4.6). The idea with .NET Standard is that a higher version is a super-set of all previous versions. This also makes sense according to this article on .NET Standard

When choosing a .NET Standard version, you should consider this trade-off:

  • The higher the version, the more APIs are available to you.
  • The lower the version, the more platforms implement it.

In general, we recommend you to target the lowest version of .NET Standard possible.

@pardahlman
Copy link
Owner

My gut feeling is that something has changed in Microsoft.Extensions.DependencyInjection. However, I will be more than happy to accept a PR that fixes this issue without changing target framework.

@glenthomas
Copy link

It does appear that a change to the DI is the cause and the only way around it (without .NET Standard 2.0) would be to add a new method of building the BusClient object graph that doesn't use the .NET DI library. Maybe could allow people to use alternative containers like structuremap/autofac/ etc.

@jwoodman510
Copy link

jwoodman510 commented Sep 6, 2017

Facing the same issue here as I have just upgraded the majority of my projects to 2.0

Would it be possible to release a separate version, or even a separate package, that specifically targets .NET Standard 2.0?

*Note:

As a workaround, I've added the following lines in place of BusFactory.CreateDefault:

            var provider = new ServiceCollection()
                .AddRawRabbit<AdvancedMessageContext>(config, custom)
                .BuildServiceProvider();

            var client = provider.GetService<IBusClient<AdvancedMessageContext>>();

            return new RawRabbit.vNext.Disposable.BusClient<AdvancedMessageContext>(client);

As long as the assembly running these lines is using .NET Standard 2.0, you will not hit a runtime exception as the IServiceCollection.BuildServiceProvider() method is not run.

@pardahlman
Copy link
Owner

So this comment sums it up

You need to update to ASP.NET Core 2.0. Anything that called BuildServiceProvider needs to be updated.

Unfortunately, Microsoft.Extensions.* packages depends on netstandard2.0, and by upgrading the RawRabbit vNext package, the extensions package needs to be upgraded as well. The lowest level of .NETFramework that supports netstandard2.0 is 4.6.1 (RawRabbit suppoort 4.5.1 atm).

Not sure how to resolve this issue. The work-around (above) seems like a acceptable workaround for now.

@Manny651
Copy link

How about having multiple target frameworks in the csproj?

@pardahlman
Copy link
Owner

Hi @Manny651 - thanks for the input. RawRabbit already has multiple targets, which is part of the problem. Can you elaborate on what you mean?

@srinathgnath
Copy link

@jwoodman510 How to set Rawrabbit configuration with this approach?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants