System.IO.FileLoadException: Could not load file or assembly Log4net - c#

I added an existing project to my solution. When I run all unit tests with MSTest runner, I get the following error on a couple of tests:
Message: Test method soandso threw exception:
System.IO.FileLoadException: Could not load file or assembly 'log4net, Version 1.2.12.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a' or one of it's dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT:0x80131040)
I know others have had the same problem and there is other questions and answers about this topic. But I tried many things, but nothing helped.
The version of log4net we use is 1.2.13.0.
I checked with FUSLOGVW.exe for binding errors. The log4net shows up with the added assembly and some Unknown assembly.
The reference of log4net in the assembly shows version 1.2.13.0 which was added with NuGet. So it is probably a dependent assembly that is causing all this trouble.
I tried changing log4net back to version 1.2.12.0, but I still get the same error message.
When I run all the tests with MSTest testrunner, these errors show up. When I only run the failed tests, they pass. When I run them individually, they also pass.
I tried binding redirection, but I did it for the assembly that is tested not the testing assembly. I did not know how to do that for a test assembly - there is no config.
When I run the tests with Resharper test runner they also pass (but other tests fail). The TFS Build server runs the MSTest-runner, therefore I need to get it working with MSTest.
Does anyone know how I can get this resolved?

The culprit was found. Like I was suspecting, a dependent assembly was referencing log4net 1.2.12.0. The tool used to find out was dotPeek. Luckily we do have the source code for the dependent assembly and we can change it to reference the latest log4net. This solves this issue.
So if anyone has a similar problem, use dotPeek or a similar tool to go through the dependent assemblies to find out what versions of assemblies it is referencing.

Related

NUnit3TestExecutor discovered 0 of 1 NUnit test cases using Current Discovery mode, Explicit run

When I run my SpecFlow+NUnit tests from Test Explorer, all tests always run, even if I have selected only some of the tests.
I also see this message which I suspect is related:
NUnit3TestExecutor discovered 0 of 1 NUnit test cases using Current Discovery mode, Explicit run
In addition, after the tests run, they are still marked as "Not run", even if they succeeded.
How can I fix this issue?
I fixed it by upgrading all the nuget packages in the solution to the latest version following advice in https://github.com/nunit/nunit-vs-adapter/issues/125. I don't know why that fixed it, but it did.
After upgrading the nuget packages I got an error:
System.IO.FileLoadException : Could not load file or assembly 'nunit.framework, Version=3.13.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
This was fixed by following advice at https://github.com/SpecFlowOSS/SpecFlow/issues/2387:

Error while running unit test cases through Jenkins: Could not load file or assembly 'Microsoft.QualityTools.Testing.Fakes, Version=11.0.0.0'

Written unit test cases using MS Test Framework for our project which is on C# and using Fakes to bypass the DB call. Test cases are running fine in Visual Studio 2015.
But when try to run through Jenkins Job it’s giving below error message:
Error Message:
Test method threw exception:
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.QualityTools.Testing.Fakes, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
Could anyone please guide to fix the above error.
The root cause of the issue - Microsoft.QualityTools.Testing.Fakes version 11.0.0.0 was missing in bin/debug in Git repository. Additionally all dll's which are referred for unit test project needs to manually added under bin/debug folder in the Git

ILMerge: How to merge Azure runtime library?

I'm trying to use ILMerge to create a single executable that uses Windows Azure SDK 2.0 including the Microsoft.WindowsAzure.ServiceRuntime.dll, which I added manually because I couldn't find any NuGET package that contains it.
I'm using the following command line on a VS2012 post-build event:
/wildcards /targetplatform:"v4,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0" /log:ILMerge.log /closed Output.exe *.dll /out:Executable.exe
However, I'm getting the following error in ILMerge.log, even though the output window reports no issues at all after building:
The assembly 'Microsoft.WindowsAzure.ServiceRuntime' was not merged in correctly. It is still listed as an external reference in the target assembly.
An exception occurred during merging:
ILMerge.Merge: The assembly 'Microsoft.WindowsAzure.ServiceRuntime' was not merged in correctly. It is still listed as an external reference in the target assembly.
at ILMerging.ILMerge.Merge()
at ILMerging.ILMerge.Main(String[] args)
And then the executable throws the following exception when opened:
System.Reflection.TargetInvocationException: Exception has been thrown by the
target of an invocation. ---> System.IO.FileNotFoundException: Could not load file
or assembly 'msshrtmi, Version=2.0.0.0, Culture=neutral, publicKeyToken=31bf3856ad364e35'
or one of its dependencies.
I have also tested the same output without merging and it works fine.
What am I missing here?
It would seem like this cannot be done, or at least not how I want it done.
Microsoft.WindowsAzure.ServiceRuntime.dll seems to have a dependency on the native library msshrtmi.dll. This means that ILMerge cannot handle it, since unmanaged code cannot be merged with managed code.
According to these two questions there is a way to work around this problem, but it involves packaging the msshrtmi.dllas a resource and unpacking it when it's needed. I feel it's kinda hacky.
So I guess I'll just have to do what I should have done before: remove all references to Microsoft.WindowsAzure.ServiceRuntime.dll from the project that builds the merged, non-Azure version of my WCF service. This might involve using some IoC container and doing some heavy refactoring, which is why I was trying to avoid it at this stage.

Unit test fails saying could not find 'Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight' assembly

Unit test(MS test framework) runs successfully in the system in which it is written but fails in other systems and throwing the below error::
"Unit Test Adapter threw exception: Could not load file or assembly
'Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight,
Version=5.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or
one of its dependencies. The system cannot find the file specified.."
'Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight' is already added as reference in my project but still the test cases are failing.Am I missing any other references?
Need help.Thanks in advance.
The issue is resolved by adding the dll to GAC using gacutil -i <path of the dll>.
e.g.:
C:\Program Files (x86)\Microsoft SDKs\Silverlight\v5.0\Toolkit\dec11\Testing>gacutil -i Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight.dll
I don't agree with above answer, even OP has marked it, please check this thread: http://www.devexpress.com/Support/Center/Question/Details/Q286983
But neither our Silverlight assemblies nor framework ones should be added to the GAC.
So uninstall the assembly you had installed in GAC.
I hope this can be helpful.

asp.net 3.5 referencing project with assembly reference throws signing/strong name error in Unit Test

I have a reference to a MySQL.Data 5.2.3 assembly in a data layer, great. I currently I have small console app inteh same solution referencing JUST THIS data layer which connects just fine. I then created a unit test project (also in the same solution) and reference that same data layer project, and from that I get:
Test method LTTests.WrapperTest.LoginTest threw exception: System.IO.FileLoadException: Could not load file or assembly 'MySql.Data, Version=5.2.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. Strong name signature could not be verified. The assembly may have been tampered with, or it was delay signed but not fully signed with the correct private key. (Exception from HRESULT: 0x80131045).
So I'm trying to understand...I can do this for a console exe and it works but not a unit test? This makes me nervous to build on something apparently flawed, but I'm at a loss for what to do next. I'm lost, I've been re-adding various things looking for what the deal is and I have no clue.
The exception is from the data layer and not from the test (per the stack) so it's like the test is calling the layer's method (duh) and the data layer is puking but not for the console?
Thanks.
This is not the answer why there is an error but a possible way to find it out:
You can use the "Assembly Binding Log Viewer" (Fuslogvw.exe) that belongs to the dotnet sdk to find out what is loaded and why.
If logging is enabled it can show you wich assemblies are loaded from which directory and which assembly caused it to load.
For details see http://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.80).aspx
so may be there are different version of MySql.Data lying around and the error message means i.e. that the version number found is not the one it expects.

Categories