Efcore: Method not found: Microsoft.Extensions.DependencyInjection.IServiceCollection

Created on 17 May 2017  路  29Comments  路  Source: dotnet/efcore

From issue #8426, I ended up upgrading EFCore versions. I ran the following package manager commands in my web and DAL projects:

install-package Microsoft.EntityFrameworkCore.SqlServer -Pre -Version 2.0.0-preview1-final
install-package Microsoft.EntityFrameworkCore -Pre -Version 2.0.0-preview1-final

I then went to run my web application and got the following exception in the Program.cs file when it tries to execute

var host = new WebHostBuilder()
    .UseKestrel()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseIISIntegration()
    .UseStartup<Startup>()
    .Build();

Method not found: 'System.IServiceProvider Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(Microsoft.Extensions.DependencyInjection.IServiceCollection)'.

I'm assuming the upgrade has broken something in my Startup class in my web app.

area-docs closed-external closed-question

Most helpful comment

@ajcvickers i am getting this error very frequently, because of package dependencies.
Project A references a package that has use v1 of package B. in other project some other package is using version 2 of B which results into this situation.
and detecting which package is causing problem is some how impossible in large project.
any idea?

All 29 comments

You need to upgrade all your packages from Asp.net core and not just the EF ones.

@ajcvickers i am getting this error very frequently, because of package dependencies.
Project A references a package that has use v1 of package B. in other project some other package is using version 2 of B which results into this situation.
and detecting which package is causing problem is some how impossible in large project.
any idea?

I had this problem after I upgraded Microsoft.Extensions.DependencyInjection.Abstractions to version 2.0.0 then reverted back... Since the assembly is still on the hard drive it is still detected and loaded. The solution was to delete the bin folder and build a gain.

Ditto what @assilsicom said. I also reverted back to Core 1.1 from 2.0 and was getting this error. Deleting the bin folders worked as stated above and resolved the issue.

I have a .NET Standard 1.4 library that has Microsoft.EntityFrameworkCore.Sqlite 1.1.2 dependency
There is a UWP project that references it and it works fine
There is a .NET Core 2.0 project that references it and it gets this error

Please stop closing valid issues, this issue still exists

Reopening so that we can write a proper response here. Using 1.x & 2.x packages of Asp.net in same project is not supported.

running into same issue here, except I am using a service fabric stateless webapp, I read SF isn't compatible with AspNetCore 2.0 yet. Do i have to roll back because everything seems to be interdependent and a massive breaking change

I'm also running into this issue. The moment i hit "services.AddIdentityServer()" i'm getting the error.

Having the same issues as @JKeetman , Just updated to to .NET Core 2.0 and IdentityServer4-preview4

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

Same issue here. I've updated everything to 2.0. Also updated IdenetyServer 4 to 2.0.0-preview2. Any suggestions?

Same Error with ASP.NET Core 2.0 and IdenetyServer4 1.5.2

@gato-negro - IdentityServer4 1.5.2 uses ASP.NET core 1.1.x packages. 1.x to 2.0 is breaking change hence ASP.NET core 2.0 is not compatible with IdentityServer 1.5.2. You would need at least IdentityServer4 2.0.0-preview3 packages to use with ASP.NET Core 2.0

@smitpatel You are right. I updated all identityserver4 related packages to 2.0.0-rc1 and now it works as expected. Thanks

what if we have a library that Called BuildServiceProvider ? what Should replace it?

what if we have a library that Called BuildServiceProvider ? what Should replace it?

The library needs to be updated.

Ok, let's say I am the one responsible to do that, and I know where in my code base we are calling it, what would I need to change it to, to work now? Or where can I go that will help me figure that out? Because I'm almost positive that the library won't officially be updated anytime soon.

The library just needs to reference the new version of Microsoft.Extensions.DependencyInjection (the 2.0.0 version).

Ahh perfect, thank you

Just curious as to how the dependency tree is allowed to get into an incompatible scenario like this - is there something more that can be done via semver and nuspec versioning constraints on packages to prevent this kind of thing from happening?

@dazinator yes we really should update all of the 1.x packages to have an upper bound of 2.0.0) at this point but we haven't.

Here is the most basic repro of this issue: https://github.com/dazinator/Repros/tree/BuildServiceProvider
Just run the console app.

It means if you have a library, and it calls BuildServiceProvider and you want it to support asp.net core 1 and 2, you need to remove any calls to BuildServiceProvider. Then it can keep its dependency on Microsoft.Extensions.DependencyInjection version 1, and it should continue to work even if version 2 used at runtime.

In my case - I now allow the consumer of my library to pass in a delegate that makes the call to BuildServiceProvider instead.

.. and lastly.. here is a basic example of the workaround described above for a re-usable library scenario: https://github.com/dazinator/Repros/tree/BuildServiceProvider-Workaround

@dazinator ... fyi a different solution for the issue, using Reflection https://github.com/peachpiecompiler/peachpie/commit/eb9213f174fa909459ad0137ff996876eac2ac4c

@divega Can this be moved out of the 2.1 milestone. (We are trying to make 2.1 only include issues that we must fix for 2.1)

The thread contains several questions and workarounds about how to have code that can work with 1.x and 2.x versions of Microsoft.Extensions.* and ASp.NET Core. Couldn鈥檛 find anything actionable for EF Core.

I'm currently getting this exception, trying to use the latest EntityFrameworkCore 1.1.x. I don't quite understand what the workaround is. Here are the currently dependencies:

image

Do I need to make sure everything is Microsoft.Extensions is 2.0.x instead of 2.1.x?

I tried downgrading `Microsoft.Extensions.Configuration.FileExtensions to 2.0.0, but I still get:

Unhandled Exception: System.MissingMethodException: Method not found: 'System.IServiceProvider Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(Microsoft.Extensions.DependencyInjection.IServiceCollection)'.
   at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_1.<GetOrAdd>b__2(Int64 k)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options)

@ctaggart Looks like you have a mix of 1.1, 2.0, and 2,1 packages. You'll need to choose one and be consistent. Also, if using .NET Core, then make sure the latest .NET Core SDK is installed.

Was this page helpful?
0 / 5 - 0 ratings