grpc-dotnet icon indicating copy to clipboard operation
grpc-dotnet copied to clipboard

Grpc.Net.ClientFactory in unit test causing exception after upgrade to .Net 6.0: The type 'ServiceCollection' exists in both 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' and 'Microsoft.Extensions.DependencyInjection, Version=3.0.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'

Open ncipollina opened this issue 2 years ago • 4 comments

What version of gRPC and what language are you using?

<PackageReference Include="Grpc.Net.Client" Version="2.40.0" /> <PackageReference Include="Grpc.Net.ClientFactory" Version="2.40.0" /> C#

What operating system (Linux, Windows,...) and version?

Windows 10

What runtime / compiler are you using (e.g. .NET Core SDK version dotnet --info)

.NET 6.0.100

What did you do?

We had a Unit Test project that we upgraded from .Net 5.0 to .Net 6.0 where we were unit testing a IServiceCollection extension method to inject a gRPC client to the dependency injection container. The unit test in question looks like the following:


        [Fact]
        public void AddNotificationSystemClient_NullAction_ThrowsException()
        {
            var s = new ServiceCollection();

            Action act = () => s.AddNotificationSystemClient((Action<NotificationSystemOptions>)null);

            act.Should().Throw<ArgumentNullException>();
        }

The issue is that Grpc.Net.Clientfactory 2.40.0 has a reference to Microsoft.Extensions.Http 3.0.3 which causes the following error when building.

The type 'ServiceCollection' exists in both 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' and 'Microsoft.Extensions.DependencyInjection, Version=3.0.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'

What did you expect to see?

I expected to be able to compile and run the unit test.

What did you see instead?

When building the project, I get the following build error:

The type 'ServiceCollection' exists in both 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' and 'Microsoft.Extensions.DependencyInjection, Version=3.0.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'

Anything else we should know about your project / environment?

ncipollina avatar Nov 10 '21 15:11 ncipollina

Possible work around: try adding an explicit PackageReference to Microsoft.Extensions.DependencyInjection >= 6 to the unit test project (or the business logic project that the unit test referneces). E.g.,

<ItemGroup>
  <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
</ItemGroup>

That should cause NuGet's pick lowest behavior to see that the lowest acceptable version is no longer 3.0.3.0 but 6.0.0.

I have no idea how this project's maintainers want to resolve this long term, if at all.

chwarr avatar Nov 10 '21 17:11 chwarr

This worked! Thanks @chwarr! I would be curious if there is a fix that could be implemented, but this work-around will allow me to continue for now.

ncipollina avatar Nov 10 '21 17:11 ncipollina

This sounds like a NuGet or assembly loading problem. The lowest valid version for all apps should be chosen, which is 6.0.0 here.

There isn't anything to fix in gRPC packages.

JamesNK avatar Nov 10 '21 18:11 JamesNK

Triage: looks like there is an old reference: https://github.com/grpc/grpc-dotnet/blob/c1d3bbfccd0be49c2b711c948cad7db90bd81b65/build/dependencies.props#L15

This should be 6.0.0 or later because of the move of ServiceCollection to the Abstractions lib (https://github.com/dotnet/runtime/pull/52284).

adityamandaleeka avatar Nov 10 '21 21:11 adityamandaleeka