Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Standard 2.0 - Which Microsoft.Extensions.DependencyInjection.Abstractions Version to Use

I've created a .NET Standard 2.0 class library which has an extension method that adds service registrations to the IServiceCollection instance. Something like this:

using Microsoft.Extensions.DependencyInjection;

namespace Cache.Extensions
{
    public static class Extensions
    {
        public static IServiceCollection AddCacheServices(this IServiceCollection services)
        {
            services.AddTransient<IMyService, MyService>();

            return services;
        }
    }
}

In order to reference the type 'IServiceCollection' I was prompted to install the 'Microsoft.Extensions.DependencyInjection.Abstractions' package. Version 2.2.0 was installed into my .NET standard library. When I created a NuGet package for my library and a user tried to install it in their ASP.NET Core 2.1 web application, they got an error indicating they had to use a version of 'Microsoft.Extensions.DependencyInjection.Abstractions' with a version 2.1 or something along those lines. This got me thinking that the version of the 'Microsoft.Extensions.DependencyInjection.Abstractions' package installed should match the .NET Core version. Is this correct? I was under the assumption the .NET package versions should match the version of the .NET Core app.

So this means referencing 'Microsoft.Extensions.DependencyInjection.Abstractions' with version 2.2.0 in .NET Standard library stops .NET Core apps with a version of 2.1 from using the app. Is this correct?

like image 535
Singularity222 Avatar asked Oct 19 '25 22:10

Singularity222


1 Answers

When you create an ASP.NET Core 2.1 web application, you get a project file like this:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>
</Project>

The Microsoft.AspNetCore.App NuGet package version 2.1.11 depends on Microsoft.Extensions.DependencyInjection, with an explicit version constraint of "(>= 2.1.1 && < 2.2.0)".

If you add a dependency directly to the web application of:

<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version=">= 2.2.0" />

then you end up with one warning (rather than an errror):

Detected package version outside of dependency constraint: Microsoft.AspNetCore.App 2.1.1 requires
Microsoft.Extensions.DependencyInjection.Abstractions (>= 2.1.1 && < 2.2.0)
but version Microsoft.Extensions.DependencyInjection.Abstractions 2.2.0 was resolved.

Now I suspect that that's fine... but it would be better for you to make your library depend on the earliest version (within the same major version) of the dependency injection package that contains all the functionality you need. It's unfortunate that Microsoft.AspNetCore.App has an upper bound in the constraint here - it doesn't make much sense (IMO) to pin to a specific minor version, given that 2.2.0 should be compatible with 2.1.x, if SemVer has been followed properly. Still, the constraint is there, and that's what's causing the warning.

If 2.0.0 (of the dependency injection abstraction package) contains everything you need, I'd just use that. Each ASP.NET Core web application will use the version that it wants, greater than or equal to 2.0.0, and all should be well for multiple versions of ASP.NET Core, with no warnings.

like image 69
Jon Skeet Avatar answered Oct 22 '25 12:10

Jon Skeet