Only NUnit error: Could not load file or assembly 'EntityFramework' - entity-framework

I get an error message in NUnit when I try to run a specific test. The error looks like this:
ChatProj.Tests.MessageRepositoryTests.Logg_LoggWorking_AssertView:
System.IO.FileLoadException : Could not load file or assembly 'EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Here is a picture
I've looked at the 2 projects in my solution, and I'm refrencing EF 6.0.0.0 in both. I've tried to look up the PublicKeyToken=b77a5c561934e089 and this is what I found in my solution. It's extra weird because I've no problem debugging or building the webapp, and the other unittests are testing within the same project (same EF version) and not reciving this error (as you can see in the picture from before).
Any idea what could be causing this problem?
UPDATE:
I think I figured it out. I was using nuget.org/packages/FakeDbSet which was using a command like : var mockItemList = new InMemoryDbSet { new Message {MessageID = 5000, Name ="Erland", MessageString ="Foo Bar", MessageDate = DateTime.Now} }; which somehow is an old way of faking entity framework code. Can be seen here: i.imgur.com/f6dnJbq.png

First, ensure that the EF DLL is actually getting pulled into the bin folder for the test application. If it is and the DLL is for EF v6, do you see version redirects for EF in your app.config for the test project? Something like this?
<configuration>
<!-- Leave the rest of the configuration file alone and just add the runtime area as follows: -->
<runtime>
<assemblyBinding>
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
If not, try pasting that into the app.config.
Sorry, it's hard to fully diagnose without more information.

Related

System.IO.FileLoadException: Could not load file or assembly 'log4net

We are using Crystal Report In MVC.NET project refer to this link : to create a PDF file from the data and export it using Crystal Reports in MVC.NET.
• https://www.c-sharpcorner.com/article/use-crystal-report-in-mvc-net/
we have this error after exporting report and try to run it :
System.TypeInitializationException: The type initializer for 'CrystalDecisions.CrystalReports.Engine.ReportDocument' threw an exception. ---> System.TypeInitializationException: The type initializer for 'CrystalDecisions.Shared.SharedUtils' threw an exception. ---> System.IO.FileLoadException: Could not load file or assembly 'log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=692fbea5521e1304' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Most likely, you compiled the app using 'AnyCPU' as the target.
Change to a specific target (x86 or 64).
Ensure the Crystal runtime matches that target.
Ensure the web app pool is also set for that target.
This is what worked for me:
Make note of some details
From the error, record the:
.dll file
Version
PublicKeyToken
for later use.
Confirm that the .dll exists
Find the project in VS, right click and “Open Folder in File Explorer”. Then go into ../bin/Debug and search for the .dll file that is causing the exception.
If the .dll doesn’t exist then this is not the correct fix and something else is awry.
Get the correct assembly version the .dll is using
The version in my .dll was 4.0.1.0.
If you do not already have ILSpy installed, open the microsoft store on your PC and download it (it’s free). Then drag the .dll file in question into ILSpy and search for the line that reads:
[assembly: AssemblyVersion("4.0.3.0")]
As you can see, there is an incongruence between the versions. This is what is causing the error.
Update app.config
Back in VisualStudio, open the app.config file in the failing project and add this snippet within :
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Configuration.ConfigurationManager" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Ensure that the publicKeyToken is the value you recorded and that you set the oldVersion upper range value and the newVersion value to the assemblyVersion found in the .dll file. Also, make sure that the version shown in the error is within the range that you’re setting (i.e. it is an older version).
All set. This should work.

Nunit3 shows System.AggregateException from an assembly that does not exist with that version

This is the error:
Message: System.AggregateException : One or more errors occurred.
----> System.IO.FileLoadException : Could not load file or assembly 'Microsoft.AspNet.SignalR.Client, Version=2.1.2.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The
located assembly's manifest definition does not match the assembly
reference. (Exception from HRESULT: 0x80131040)
We are only running version 2.3.0 of Microsoft.AspNet.SignalR.Client.
I ran a grep over the whole solution and could not find a single instance which does not use 2.3.0. or runs the version mentioned in the error
The test itself does not have a reference to this library
I deleted all bin folders, cleaned and rebuilded.
Update:
I am pretty sure the problem is not nunit. Rather it shows the symptons. How do you go about finding out where this reference is used? App.configs for example only show ranges:
<assemblyIdentity name="Microsoft.AspNet.SignalR.Client" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.3.0.0" newVersion="2.3.0.0" />
I added an app.config in the test project and in the project that the test is running in which I declared:
<assemblyIdentity name="Microsoft.AspNet.SignalR.Client" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.3.0.0" newVersion="2.3.0.0" />
Now the exception is gone. Still I don't think this is the right solution. Normally visual studio must know which assembly version is the right one without declaring an app.config every time.

MsTest ignoring assemblyRedirect in app.config

I have a project that I recently upgraded the json nuget package to v10.
The same project also has a reference to System.Net.Http.Formatting (via Microsoft.AspNet.WebApi.Client.5.2.3 package) - which references json v6.0 dll.
I have an app.config in the test project that has:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"
culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>
When I run the tests in VS, all tests pass.
But when I run the same tests using MsTest command (ver 12 in C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE), a bunch of test methods fail with the exception:
System.IO.FileNotFoundException: System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified
This is the exact line in the test method where it fails:
HttpRequestMessage.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
I have checked my entire solution and confirmed that none of the projects have any reference to json v6.
Is there a reason why the redirects are being ignored when I use MsTest ?
(I can see the testProjectName.dll.config with the redirect in the bin folder of the test project too)
Also, I read that using a testsettings file can resolve this issue, but I am unclear what I need to have in my testsettings file to redirect to the newer json.

PowerTools Beta 3 fails

I was trying to generate "views" to enhance the performance of my application. The application is Silverlight 4 linked with RIA services to the Server. The server application started with EF 4.1 + RIA Services.
I installed "EF Power Tools Beta 3 v0.7.0.0" and could generate my views with no problem. But I soon realized that EF 4.1. was not able to benefit from the precompiled views. EF >= 4.2 was required.
Through NuGet I saw an update for RIA Services (RIAServices.EntityFramework.4.2.0) which used EF 5. I installed and got everything working but when I try to generate views I get an exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
I've tried reinstalling PowerTools. No luck
I've downloaded source code for PowerTools and debugged the Exception. Apparently PowerTools was trying and could not find the old EF version. The LoaderExceptions property read:
{System.IO.FileNotFoundException: Could not load file or assembly 'EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies.
I've investigated the PublicKeyToken and confirmed that it is the same as the EF 5 (EF 4.4 target=.NET 4) dll found in my "bin" output map. But PowerTools is looking for EF 4.1.. why?
My web.config file has this:
<sectionGroup name="system.serviceModel">
<section name="domainServices" type="System.ServiceModel.DomainServices.Hosting.DomainServicesSection, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" allowDefinition="MachineToApplication" requirePermission="false" />
</sectionGroup>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
and:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.4.0.0" newVersion="4.4.0.0" />
</dependentAssembly>
</assemblyBinding>
I've also tested changing the PublicKeyToken in Web.config just to confirm that I get another error from PowerTools thus PowerTools seems to parse my Web.config.
Update:
I've created a new Web project and added all Model code files as links. Same connectionstrings. Now i can build views.. strange.. same references.
When I add the views to my original project the RIA services won't start. Removed views and they started again.

Is it possible to use RIA Services and EntityFramework.Migrations together in a project?

On NuGet there are packages:
EntityFramework.Migrations
EF 4.3.1
EF 4.1
RIA Services, which depends on EF < 4.2.
EntityFramework.Migrations doesn't work, because it is now included in EF 4.3+
I'm using Code First approach, so I need the DbDomainService<> class, which is not available, when RIA Services aren't installed.
Are there any standalone downloads for the above to manually reference in my project?
EDIT:
I removed RIA from NuGet packages, updgraded EF to 4.3.1 and then referenced RIA EntityFramework lib from toolkit manually as suggested here: Can I use RIA Services with Entity Framework 4.3?
Unfortuately now I'm getting the following error:
The following exception occurred creating the MEF composition container:
Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
The default code generator will be used.
I guess that your problem is related to the T4 code generator of Ria Services. You can however get an idea of the type it can't load attaching your debugger (if you use VS don't forget to "Break on ALL Thrown CLR Exception) to the compilation of client side assembly (msbuild MyClientProjectThatContainTheProxy.csproj. )
However, if you just want to "try something", put this
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="4.3.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
in your web.config, to tell the CLR how to resolve depndencies.
More info here: http://mcasamento.blogspot.it/2012/10/entity-framework-5-code-first-and-wcf.html