AspNetCore.Mvc.Core Version Mismatch - c#

I'm attempting to write a unit test for a Controller extension method, but during the Test project's build process I receive the following error message.
MyControllerExtensionsTests.cs(17, 26): [CS1705] Assembly 'MyApp.RestApi' with identity 'MyApp.RestApi, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' uses 'Microsoft.AspNetCore.Mvc.Core, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' which has a higher version than referenced assembly 'Microsoft.AspNetCore.Mvc.Core' with identity 'Microsoft.AspNetCore.Mvc.Core, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
Both projects declare <TargetFramework>netcoreapp2.1</TargetFramework> in their .csproj files. I've tried deleting bin and obj folders. How is one project resolving to Mvc.Core, Version=2.1.0.0 and another Mvc.Core, Version 2.1.1.0?

<PackageReference Include="Microsoft.AspNetCore.App" />
I had the same issue, after add this line to unit test project, it start pick right version of Microsoft.AspNetCore.App.

Update
The issue noted below has been fixed & you should be able to benefit from implicit package versioning and reference like below without providing the version number of the package.
<PackageReference Include="Microsoft.AspNetCore.App" />
Original Answer
This issue is because of the Implicit Versioning that was introduced for Microsoft.AspNetCore.App metapackage. With implicit versioning the sdk decides the version & it resolved it as 2.1.1
However, it was resolving to version 2.1 for the nunit test project. Specifying the version number for the nunit project like <PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.1"/> and performing dotnet restore helped resolve this issue.
There is a ticket for this issue in github and this behavior around implicit versioning could change in future - https://github.com/aspnet/AspNetCore.Docs/issues/6430 & https://github.com/dotnet/core/blob/master/release-notes/1.0/sdk/1.0-rc3-implicit-package-refs.md

Related

Dependencies missing when NUnit executes MVC controllers unit tests on .NET 6

I have two projects and the both are developed in .Net 6.
The first one is a kind of REST API and contains MVC controllers.
The second one is a NUnit project that contains unit tests of the MVC controllers of the first project.
When I execute my unit tests on Visual Studio (with ReSharper), everything is working well.
But when I try to execute those unit tests on the CMD with Cake, I got this error.
Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.AspNetCore.Mvc.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
Could not load file or assembly 'Microsoft.AspNetCore.Mvc.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
----> Could not load file or assembly 'Microsoft.AspNetCore.Mvc.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
----> Could not load file or assembly 'Microsoft.AspNetCore.Mvc.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
Which says that one dependency is missing.
The first thing I tried is to add the missing dependency with NuGet.
But I cannot add the missing dependency via NuGet to my test project because the missing dependency is only available on .Net Framework 4.5.1 and higher (https://www.nuget.org/packages/Microsoft.AspNet.Mvc.Abstractions/6.0.0-rc1-final). And the dependency that I can install, is not the requested one (https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Abstractions/2.2.0).
I also try to change the project type inside my .csproj from <Project Sdk="Microsoft.NET.Sdk"> to <Project Sdk="Microsoft.NET.Sdk.Web"> but it didn't worked.
I also try to add different package references like <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" /> and <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.13" /> but it didn't worked too.
I see that it is possible to create integration tests with xunit but I don't want to make integration tests and I want to keep NUnit (https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0).
The only workaround that I found and which is working; it's to download the missing dependency as a DLL on the NuGet website and add them to the project references (https://nuget.info/packages/Microsoft.AspNet.Mvc.Abstractions/6.0.0-rc1-final). But I was wondering if there was a proper way. Maybe a NUnit configuration or something like that that I'm missing.
Have you experienced this problem before and know what I could do to fix this?
Thanks in advance

DLL in nuget references Version=0.0.0.0

I have the following situation:
There is a library LibF which I build as a nuget package, it contains dlls for net48 and net core 3.1. I am the person packing it directly from the csproj using:
<PropertyGroup>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
It has the version="4.2.0-alpha.55" (I use GitVersion). Then I have another library LibD that uses LibF, so it references it as a nuget package in the .csproj like this:
<ItemGroup>
<PackageReference Include="LibF " Version="4.2.0-alpha.55" />
</ItemGroup>
Then I also create a nuget package from LibD. I am the person packing it directly from the csproj using:
<PropertyGroup>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
When I try to consume the nuget of LibD somewhere else I get a compile issue in Visual Studio 2019:
Error CS0012 The type 'MyType' is defined in an assembly that is not referenced.
You must add a reference to assembly 'LibF , Version=0.0.0.0, Culture=neutral,
PublicKeyToken=468d6536c503beba'.
Obviously I don't have version 0.0.0.0. Now when I look at the nuget package (unzip) I see the following entry in the LibD.nuspec:
<dependency id="LibF" version="4.2.0-alpha.55" exclude="Build,Analyzers" />
This is correct. But when I use JetBrains DotPeek to analyze the dll it tells me in the references of the dll:
LibF, Version=0.0.0.0, Culture=neutral, PublicKeyToken=468d6536c503beba
So my question is: Why is there Version=0.0.0.0 as a reference for the dll? Why does it not want 4.2.0.0 because that is the version I have and the version I would expect to be referenced by the dll.
What could lead to such an issue?
What was missing was the following in the PropertyGroup of the csproj:
<UpdateAssemblyInfo>true</UpdateAssemblyInfo>
Afterwards version of assembly is correct.

Unable to load System.Threading.Tasks.Extensions

I have a web project build on .net framework 4.5.1. We are trying to added PostgreSQL support for the project. Using Nuget, I have installed 4.0.4 npgsql to the project. Under references, I see the following being added to the project.
Npgsql - 4.0.4.0 - Runtime version v4.0.30319
System.Threading.Tasks.Extensions - 4.2.0.0 - Runtime version v4.0.30319
When I tried run the project and connect and get the data from the database, I am getting the following error saying FileNotFoundException:
System.TypeInitializationException
HResult=0x80131534
Message=The type initializer for 'com.rsol.RConfig' threw an exception.
Source=RConfig
StackTrace:
at com.rsol.RConfig.getInstance() in C:\Workspaces\PS\RConfig\RConfig.cs:line 1113
at RAdmin.Global.Application_Start(Object sender, EventArgs e) in C:\Workspaces\PS\RAdmin\Global.asax.cs:line 528
Inner Exception 1:
TypeInitializationException: The type initializer for 'com.rsol.Db.DbMgr' threw an exception.
Inner Exception 2:
FileNotFoundException: Could not load file or assembly 'System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
Inner Exception 3:
FileNotFoundException: Could not load file or assembly 'System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
System.Threading.Tasks.Extensions which is installed using Nuget is not getting loaded to the project. When I checked the properties of System.Threading.Tasks.Extensions reference, the dll file exists in the location. I have also tried installing System.Threading.Tasks.Extensions.dll file to assembly using gacutil. I am still getting the same error.
Please let me know if you need any additional information.
Any help is really appreciated.
In my case, I got the issue after upgrading to version 4.5.4 and tried #user2713341 answer. It didn't work but put me in the right direction.
My project had no bindings for this library, so I added the binding and it worked
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
</dependentAssembly>
and it worked.
Note that it should be 4.2.0.1 even though the version is 4.5.4.
Update Nuget Package
https://www.nuget.org/packages/System.Threading.Tasks.Extensions/
will solve your problem
Update Nuget Package is not working for me.
What works?
In app.config need to change
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
To
<bindingRedirect oldVersion="0.0.0.0-4.5.4" newVersion="4.5.4" />
for current version 4.7.2 should work.
Microsoft like ;)
The response from #Keyjote was at the root of the solution for me, but rather than cherry-picking the assemblies, I was able to just reinstall. This seemed to automatically repair the app.config file.
Tools -> Nuget Packet Manager -> Packet Manager Console
Update-Package -reinstall -Project <your project name>
This way you don't need to mess with the syntax or have to figure out the PublicKeyToken values.
If you want to do it for the whole solution, you can omit the -Project <> parameter.
I got the error in a different context when trying to migrate using Entity Framework Core (EFC) Version 3.1.8 using the Package Manager Console. The project built successfully.
Trying the binding redirects as suggested in this thread and (re)installing different versions of System.Threading.Tasks.Extensions as well as EFC (as suggested also elsewhere) did not work for me.
I managed to solve the problem by
deleting the packages/ directory in my project's root directory
using manual NuGet Package Restore (link to Microsoft Docs) via Visual Studio
problem lies in *.csproj file. having wrong reference for System.Threading.Tasks.Extensions.4.5.4 because this dll not loading.
after referring correct framework version folder from lib its start working
old Reference:
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.2\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
corrected one:
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net472\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
please clean the nuget cache first, then run test case it will work
I have been struggling a lot with bindingRedirect.
I finally found a real gotcha that solved my problems.
I have a WCF server project in .net 4.8, depending on projects in .net standard 2.0.
I was updating Nuget packages in the referred projects.
I got al sorts of problems, I won't try to mention them here.
I had bindingRedirects in web.config. I had to rediscover that those were unnecessary, and maybe even conflicting, because of automatic generation of those into the dll.config. There are various aspects to make that work, see elsewhere.
But the real gotcha for me was that for IIS, I had to LINK web.config to the dll.config. The latter being the complete config-file, with all the bindingRedirects, which turned out to be working after all.
Try to download the package and add reference to your project explicitly . should work , I just resolved it .
I got a similar error message - but for a different reason. In packages.config set by NUGET manager There was a ref for the new version - but in project reference there was a ref for an old version. The solution - delete the ref in project reference

Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0

First of, some context information:
The platform this is running on has .Net Framework 4.7.1 installed. I have a Class Library that is in the .Net Standard 2 specification in order to support .Net Core in the future. Now parts of dependencies, namely Dapper, uses System.Data.SqlClient. This library works just fine on my own machine but I run into problems when I deploy and test it on my Windows 2012 server. Namely, I have a runtime error when Dapper is used: Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=kfddsnfsjnfs' or one of its dependencies. The system cannot find the file specified.
Mind you I first had version 4.5.1.0 installed. I then downgraded to 4.4.0.0 and rerrun the code. Now I got the same error but this time regarding 4.2.0.0. But I cannot seem to find this particular version on Nuget. After this I googled. A lot. First I tried adding rebindining the old version with a new by adding both a
appsettings.json:
{
"dependentAssembly": {
"assemblyIdentity": {
"name": "System.Data.SqlClient",
"publicKeyToken": "kfddsnfsjnfs",
"culture": "neutral"
},
"bindingRedirect": {
"oldVersion ": "4.4.0.0",
"newVersion": "4.5.1"
}
}
}
and app.config:
<dependentAssembly>
<assemblyIdentity name="System.Data.SqlClient" publicKeyToken="kfddsnfsjnfs" culture="neutral" />
<bindingRedirect oldVersion="4.4.0.0" newVersion="4.5.1.0" />
</dependentAssembly>
However it didn't make a difference. I have also tried older versions of the SqlClient and multiple reinstalls. I also found people who said to double check the csproj file so it didn't reference something in the gac, but it does not:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Authors>me</Authors>
<Product />
<Company />
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.8</Version>
</PropertyGroup>
<ItemGroup>
<Content Include="TaskMetadata.json">
<PackagePath>TaskMetadata.json</PackagePath>
<Pack>True</Pack>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="dapper" Version="1.50.5" />
<PackageReference Include="itextsharp" Version="5.5.13" />
<PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="External\" />
</ItemGroup>
</Project>
Each library runs under the process of the main application. So the main application needs to know to load the SqlClient DLL. So the parent project (the .NET 4.7.1 project) needs to have SqlClient added as a reference, either by installing the NuGet package, or just adding a reference by browsing to the DLL under the .NET Standard project.
Old answer: That version exists in NuGet: https://www.nuget.org/packages/System.Data.SqlClient/4.4.0
In the Package Manager Console (make sure the 'Default project' drop-down is set to the right one), try uninstalling and then reinstalling that specific version:
Uninstall-Package System.Data.SqlClient
Install-Package System.Data.SqlClient -Version 4.4.0
Update: Or, in your binding redirect, just use 4.2.0.0 as the oldVersion.
install version 4.8.2 in both project
Install-Package System.Data.SqlClient -Version 4.8.2
I had the same issue, Basically I was developing a win form app. with .net framework, and my class library was .net standard.
I tried install and uninstall System.Data.SqlClient ten times, it did not work
Then something later crossed my mind as I installed same reference in the UI layer as well then it worked. even though, I did not have any code talking to Sql on UI layer, then another issue raised as the UI did not recognize reference to Dapper as well, and I had to install Dapper reference on UI too. Then everything was fine.
I really surprises how that did not work. Once you referenced the UI to the class library, the class library supposed to do what is required and pass the data to UI!!
Switch from System.Data.SqlClient to Microsoft.Data.SqlClient.
https://devblogs.microsoft.com/dotnet/introducing-the-new-microsoftdatasqlclient/
Try adding System.Data.SqlClient again via NuGet although you may already have it handy as a part of the .NET Framework. This solution resolved my problem.
I was facing this issue while running unit test cases. {"Could not load file or assembly 'System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.":"System.Data.SqlClient, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"} System.IO.FileNotFoundException
When I install System.Data.SqlClient, Version=4.4.0.0 then it did not work.
But this got fixed when I installed the latest version of System.Data.SqlClient nuget package.
I was getting this error with .NET Core (using the latest Microsoft.Data.SqlClient) and had to change my DBProvider from
Microsoft.Data.SqlClient
to
Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient
in order for NLog to load Microsoft.Data.SqlClient
I was getting this error at the console in one of my webjobs in Azure. In the publish profile I changed configuration from "debug" to "release" and then it worked.

Microsoft Security Advisory 4021279 System.BadImageFormatException

My application is an ASP.NET Core 1.0 Web API.
According to the Microsoft Security Advisory 4021279, I wanted to update the direct dependicies of my project.
In the MyClassName.csproj is the following line:
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.0.3" />
which I change to:
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.0.4" />
because the versions 1.0.0, 1.0.1, 1.0.2, 1.0.3
1.1.0, 1.1.1, 1.1.2 of Microsoft.AspNetCore.Mvc are not save to use.
If I then try to run the project, Iam getting the following errormessage:
System.BadImageFormatException: "Could not load file or assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)"
Inner Exception: BadImageFormatException: Cannot load a reference assembly for execution.
What am I doing wrong?
So The post on Git says the following:
If after recompilation you see a Dependency conflict warning, you must update your other direct dependencies to the appropriate version.
For example if your project refers to Microsoft.AspNetCore.Routing with a version number of 1.0.1 when you update your Microsoft.AspNetCore.Mvc package to 1.0.4, compilation will throw:
NU1012 Dependency conflict. Microsoft.AspNetCore.Mvc.Core 1.0.4 expected Microsoft.AspNetCore.Routing >= 1.0.4 but received 1.0.1
As you can see in my question, I do not get this error message. However, I updated the version of Microsoft.AspNetCore.Routing to 1.0.4 and everything worked.

Categories