Conflict of versions after upgrading to .NET 6 with ServiceCollection - .net-assembly

Upgrading from .NET 5 to .NET 6 is proving not so straightforward as I expected.
All my unit test projects fail to compile due to the following error
Severity Code Description Project File Line Suppression State
Error CS0433 The type 'ServiceCollection' exists in both 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' and 'Microsoft.Extensions.DependencyInjection, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' Sample.DynamoDb.FunctionalTests C:\src\my-solution\test\Sample.DynamoDb.FunctionalTests\DependencyInjectionTests\GetServiceTests.cs 22 Active
They reference class libraries that depend on Microsoft.Extensions.DependencyInjection.Abstractions version 6
but for some reason it seems there is a dependency on Microsoft.Extensions.DependencyInjection in the test project itself. Probably dragged across by some of the packages I use (and I don't know which one).
This is my test project
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>Enable</Nullable>
<IsPackable>False</IsPackable>
<IsPublishable>False</IsPublishable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Sample.DynamoDb.DependencyInjection\Sample.DynamoDb.DependencyInjection.csproj" />
</ItemGroup>
</Project>
Anyone with a similar problem or an idea on how to troubleshoot and find the issue or a workaround?

It turns out the problem was because some referenced project, referenced another, that referenced another... that had a nuget dependency EventStore.Client.Grpc that depends, itself, on Microsoft.Extensions.Logging 5.0.0
And that Microsoft.Extensions.Logging bring the subdependency Microsoft.Extensions.DependencyInjection 5.0.0
Having a look at my test project's obj/project.assets.json helped me trace that.

Related

Could not load file or assembly 'System.IdentityModel.Tokens.Jwt, Version=5.6.0.0 after upgrading Azure Function's nuget packages to 3.0.4 or 3.0.5

We have had an Azure Function V3 that has a reference to another C# project in a VS 2019 solution. There are some extension methods in the other C# project using System.IdentityModel.Tokens.Jwt Version=5.6.0.0 assembly to perform token validation. The Azure Function has been working fine by using those extension methods to validate token. The following code snippet shows the nuget packages:
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.SignalRService" Version="1.0.2" />
<PackageReference Include="Microsoft.Azure.EventGrid" Version="3.2.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventGrid" Version="2.1.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.3" />
We upgraded the nuget packages in the azure function according to the following list and started receiving the exception reflected the title of this ticket. What's the solution or workaround to solve this problem?
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.SignalRService" Version="1.0.2" />
<PackageReference Include="Microsoft.Azure.EventGrid" Version="3.2.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventGrid" Version="2.1.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator" Version="1.1.4" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.4" />
We also tried Microsoft.NET.Sdk.Functions version 3.0.5 but it exhibits the same problem What is the solution for this problem? Is this an issue to be fixed in the upgraded nuget package?

Azure function v2, Core 3.1 Could not load file or assembly System.Memory, Version=4.2.0.0

I'm running locally an Azure function v2 on Core 3.1. Function connects reads events from an EventHub and write data to a Redis database.
While connecting function gets an error
System.Private.CoreLib: Exception while executing function:
One or more errors occurred. (Could not load file or assembly 'System.Memory, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.).
Pipelines.Sockets.Unofficial: Could not load file or assembly 'System.Memory, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified. System.Private.CoreLib: Could not load the specified file.
Same Redis connection code works normally as expected outside the Azure function.
I have installed System.Memory nuget package v 4.5.3 into the project but it does not help.
There is no System.Memory version 4.2 listed in nuget for Core 3.1
function uses Startup
[assembly: FunctionsStartup(typeof(...))]
The project file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v2</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventHubs" Version="4.1.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
<PackageReference Include="System.Memory" Version="4.5.3" />
<PackageReference Include="Utf8Json" Version="1.3.7" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Configuration\Configuration.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
What's wrong?
The problem was fixed by changing the Azure functions version to v3 in project .csproj file.
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
If the problem persists, verify that you are using the correct version of Azure Functions Core Tools (v3), it happened to me that I took version 2 from nodejs that is, I had version 2 installed in nodejs and version 3 in the folder From microsoft in the path they were both but I took the first one that was the nodejs one and it kept giving me the error until I delete the nodejs one, you can update it to version 3 in nodejs and I suppose it will work the same.
Can you try to add true to your .csproj file:
<PropertyGroup>
...
<UseNETCoreGenerator>true</UseNETCoreGenerator>
</PropertyGroup>

Version problem on Azure Functions v1 (using .Net461/.Net472) with NotificationHub v3.1.0

I would like to run Azure Function v1 with .Net461/.Net472 for testing NotificationHub package. However, there is exception when executing below code:
await _hub.CreateOrUpdateRegistrationAsync(registration);
Error is :
Exception while executing function: PushRegister. Microsoft.Azure.NotificationHubs: Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=XXX' or one of its dependencies. The system cannot find the file specified.
I tried to downgrade to 10.0.3 then had error like
Microsoft.Azure.NotificationHubs: Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0 ...
Below is the XXX.csproj file for reference:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<AzureFunctionsVersion>v1</AzureFunctionsVersion>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Azure.KeyVault" Version="3.0.3" />
<PackageReference Include="Microsoft.Azure.NotificationHubs" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.ServiceBus" Version="2.3.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.29" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\YYY\YYY.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Configuration" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
Most of the Nuget libraries have the dependency on Newtonsoft package, even the Microsoft.NET.Sdk.Functions has a dependency on it but it is of older version.
You cannot use any other Nuget Packages that have a dependency on Newtonsoft.Json version which is higher than the function runtime dependent version.
When you try to install a NuGet which has the dependency on a higher version of Newtsonsoft package, you simply cannot install it. The only solution is to use an older version of that library that you need.
I have also written a blog on this and on github this is already an open issue
https://medium.com/#hharan618/common-issues-while-development-of-azure-functions-76b08299af58

VSO Build Pipeline's Nuget Restore Failed

The Nuget Restore from VSO's build pipeline failed due to some nuget packages that are not compatible with netcoreapp2.1. However, when I looked through all the csproj files in the solution, none of the csproj files have these nuget packages. I might have installed these before but right now it's definitely not here.
Here's the csproj file that is mentioned in the error message.
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi" Version="5.2.6" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
</ItemGroup>
Here's the Nuget.Config file
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageRestore>
<add key="enabled" value="True" />
<add key="automatic" value="True" />
</packageRestore>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>
Please advise how to fix this issue.
EDIT=====================================================
After removing the nuget package from Microsoft.AspNet.WebApi, the NugetRestore passed, but Build solution failed. It shows a different error
[error]C:\Program Files\dotnet\sdk\2.2.104\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(208,5):
Error NETSDK1004: Assets file
'd:\agentwrok\18\s....\obj\project.assets.json' not found. Run a
NuGet package restore to generate this file.
This is the configuration for Build Solution
Any advice on the next step?
VSO Build Pipeline's Nuget Restore Failed
Just like Volodymyr commented, you should not use the package Microsoft.AspNet.WebApi for the .net core project. That is because this package is targeted at .NETFramework, not .NET Core/.NETStandard. It is not compatible with .NET Core.
When you check the package Microsoft.AspNet.WebApi on nuget.org, you will notice that this package has a dependency on Microsoft.AspNet.WebApi.WebHost, which only targets the .NET Framework:
And the sub-dependency Microsoft.AspNet.WebApi.Core of the package Microsoft.AspNet.WebApi.WebHost also only targets the .NET Framework:
So, the reason for this issue is that the package Microsoft.AspNet.WebApi and its dependencies are not compatible with .NET Core framework.
Hope this helps.

Change from netcore 1.1 to netcore 2.2

I have this old project which is in netcore 1.1 and I'm trying to run this project in my new Dev environment which is has netcore above 2.0. As you can guess when I'm trying to debug this old project my command line starts glowing like a christmas tree with all the known errors like
It was not possible to find any compatible framework version
The specified framework 'Microsoft.NETCore.App', version '1.1.2' was not found.
- Check application dependencies and target a framework version installed at:
C:\Program Files\dotnet\
and
- The following versions are installed:
2.1.6 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
2.1.7 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
2.2.0 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
So my question is how can I change my project settings without installing netcore 1.1?
Also worth to mention is that I already know how to fix this problem when working in Visual Studio with a project solution(just changing the version in project properties will fix this problem) but this project is in Visual Studio Code and it has no solution file inside the project folder.
I have already changed some settings in .csproj but without any luck.
This is what I have changed in .csproj From:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug"
Version="1.1.1" />
</ItemGroup>
</Project>
to:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
</Project>
Delete /bin and /obj folders and import Microsoft.AspNetCore.App in your .csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Api.Analyzers" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
</Project>

Resources