Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net 6 BuildServiceProvider is missing

net 6, create console application with 'minimal template'. try add DI but BuildServiceProvider is missing. I try google about how migrate ServiceCollection but did not find nothing.

var services2 = new ServiceCollection();
var serviceProvider = services2.BuildServiceProvider();

enter image description here

like image 632
padavan Avatar asked Sep 12 '25 05:09

padavan


2 Answers

In my idiotic case, I was using Microsoft.Extensions.DependencyInjection.Abstractions instead of Microsoft.Extensions.DependencyInjection. I didn't notice at first because ServiceCollection, AddSingleton etc were available using the first NuGet as well. Icon and author are the same for both NuGets.

like image 94
Francesco B. Avatar answered Sep 15 '25 04:09

Francesco B.


The following two steps fixed it for me (VS2022, .NET Standard 2.0):

  1. Make sure all your projects are referring to the same version of Microsoft.Extensions.DependencyInjection. At the time of this writing, the latest version is 7.0.0. You can use Consolidate tab in solution's Nuget Configuration page to achieve this.
  2. Go to project's Nuget Configuration page (and not Solution's Nuget Configuration page) by right-clicking the problematic project and choosing Manage Nuget Packages. Therein, uninstall Microsoft.Extensions.DependencyInjection and then re-install it (again making sure you're using the same version as other projects in the solution).

Rebuild the solution after this and things should look good.

like image 21
dotNET Avatar answered Sep 15 '25 03:09

dotNET