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

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

Related

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

How to resolve file not found when loading interop dll with .net core 3.0?

I'm trying to port some code from .net frame 4.7.1 to .net core 3.0.
The problem is that we have a C++ COM assembly that we use a manifest to reference in out .net project.
I've added the project to the new solution and added the reference. An interop dll was created, but when I try to run the code I get an exception
{"Could not load file or assembly 'Interop.MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.":"Interop.MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"}
The file is in the output folder for the project. The actual com assembly wasn't, but even copying it in doesn't work.
The answer turns out to be that adding a reference in visual studio doesn't work for .net core apps.
The solution is to create a .net framework app, add the reference, then copy the comreference sections of the csproj into your .net core project.
EmbedInteropTypes!!! took me forever to find this...!!
<Reference Include="Microsoft.Office.Interop.Excel, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
<HintPath>Microsoft.Office.Interop.Excel.dll</HintPath>
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>

AspNetCore.Mvc.Core Version Mismatch

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

Could not load file or assembly 'Ninject version 4.0.0.0

There is a problem with assembly reference, guess occurred when updating nuget packages.
I start from a project which has no dependency to most dependencies(topological sort) reinstalled all nuget packages and increase version in nuspec file and all packages.config files referenced to Ninject 3.2.2.0
<package id="Ninject" version="3.2.2.0" targetFramework="net462" />
<package id="Ninject.Extensions.Conventions" version="3.2.0.0" targetFramework="net462" />
but doesn't work.
still getting error when running unit test, seems an assembly referenced to other version of Ninject
unit test fails:
Message: System.IO.FileLoadException : Could not load file or assembly 'Ninject, Version=4.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
---- System.IO.FileLoadException : Could not load file or assembly 'Ninject, Version=3.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
how to get more informations about this(who's calling version 4.0.0.0 or 3.2.0.0) or fix this issue?
Thanks in advance.
I have the same issue. I tried to edit my project file to check my dependency versions but it is okay.
I also tried to edit my web.config file since the dependency versions are there:
In the end, these what I have done:
Uninstall all Ninject packages in Nuget Package Manager.
Remove all Ninject dependentAssembly nodes on web.config file.
Then reinstall again the Ninject packages I have before.
Everything work fine after this.
I had the same problem, I upgraded my project to 4.6.2 from 4.5 which used Ninject version 3.2.2.0, and I got the same error message.
What helped me was that I upgraded the Ninject package to the latest version (3.3.4) and Ninject.Web.Common (3.3.2).
Warning! When I did so, my NinjectWebCommon.cs file was re-generated and I lost all dependency injection settings, but once I restored it from source control, everything was fine.

Why I can not use System.Net.Http package in a solution with System.Net.Http reference?

Using VS2017, I've created a ClassLibrary (.NET Framework 4.6.2) in an empty solution. Then I've installed System.Net.Http 4.3.2 package there and used HttpClient class in a Class1 constructor.
Then I've created a ConsoleApp (.NET Framework 4.6.2), referenced ClassLibrary and instantiated Class1 in the Main method.
Now running ConsoleApp causes runtime exception:
Unhandled Exception: System.IO.FileNotFoundException: Could not load
file or assembly 'System.Net.Http, Version=4.1.1.1, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The
system cannot find the file specified. at
ClassLibrary1.Class1..ctor() at ConsoleApp1.Program.Main(String[]
args) in [...]
In detailed build log I see this message:
2> There was a conflict between "System.Net.Http, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" and
"System.Net.Http, Version=4.1.1.1, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a".
2> "System.Net.Http, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" was chosen because it was primary and
"System.Net.Http, Version=4.1.1.1, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" was not.
Let's suppose I can not remove reference to System.Net.Http from ConsoleApp, because I have this situation in a real project structure.
Playing with Specific Version parameter of System.Net.Http
reference did not help.
Specifying <bindingRedirect oldVersion="0.0.0.0-4.1.1.1"
newVersion="4.1.1.1" /> did not help
I've googled it and it is a popular problem, but I didn't find a clear explanation of what actually happens and how to fix such cases in general.
Could not load file or assembly System.Net.Http version 4.1.1.0
Could not load file or assembly 'System.Net.Http' or one of its dependencies
About conflicts:
What does .NET mean by 'primary' when choosing between conflict dll reference?
Found conflicts between different versions of the same dependent assembly that could not be resolved error
I wonder why you are choosing to use the NuGet package instead of using the Reference Manager to load in the appropriate assembly. By default in a lot of project templates VS includes System.Net.Http. If it is the case that you used the NuGet package explorer to install the assembly, then one of the two options should help out:
Remove the NuGet package and use the assigned version from the Reference Manager (look under Assemblies / Framework) though I am betting this is already selected.
In the Reference Manager un-select the assigned version of System.Net.Http and use the one that you installed with NuGet.
Personally I think option 1 is better unless you absolutely need something specific found only in the NuGet latest version.
The best and easiest way to fix this issue, is with a binding redirect, as said. But the versioning looks off.
Simply specify the oldVersion as 0.0.0.0-5.0.0.0, and newVersion as 4.1.1.0
Where 4.1.1.0 is your version, for example.

Categories