Version Independent reference in C# Class Library - c#

I have a C# class library MyLibrary in which I am referencing a sample.dll version 2.7.0. This MyLibrary is being referenced in WCF project. Which is deployed on the server.
Now on the server, I have sample.dll with the version 3.0.0 because of that I am getting referenced dll not found error as following.
Could not load file or assembly 'sample, Version=2.7.0.0, Culture=neutral, PublicKeyToken=185805eaf7302b6c' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
I have found few solutions for making it version Independent like adding 'BindingRedirect' as it is a class library, I don't have web.config or app.config where I can configure 'BindingRedirect'. I have also tried setting 'Specific Version' to false but still getting the same error.
I want to make my class library such that it should work with any version of Sample dll
I tried adding following in web.config of my WCF project
<dependentAssembly>
<assemblyIdentity name="sample" publicKeyToken="185805eaf7302b6c" culture="neutral"/>
<bindingRedirect oldVersion="2.7.0.0" newVersion="3.0.0.3"/>
</dependentAssembly>
But still getting the same error.

Related

FileLoadException Occured for System.Web.Http

I am having a generic problem with probably some unique behavior (and it has pissed me off). I am using .Net framework 4.5 with Visual Studio 2012. I am facing problem running unit tests:
FileLoadException Occured: Could not load file or assembly
'System.Web.Http, Version=5.2.0.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)
My unit test app.config has following configuration
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
</dependentAssembly>
My project (where unit test calling method) has following
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
</dependentAssembly>
Now the weird problem is, I am able to run the unit tests successfully in my machine (having VS 2013 and VS 2012 installed). However in my colleague's machine (only VS 2012 installed) it fails with above mentioned error. From where I am getting this 5.2.0.0 version number?
The solution (including unit tests) runs fine in Build servers. So I assume my machine config is fine and my colleague's machine has some problem. The project Dll is not present in GAC.
WHAT I TRIED
Deleting the base folder and taking specific in both machine. My machine works fine and other fails.
Enabled all exception settings and caught another error
The assembly with display name 'System.Web.Http' failed to load in the
'Load' binding context of the AppDomain with ID 2. The cause of the
failure was: System.IO.FileLoadException: Could not load file or
assembly 'System.Web.Http, Version=5.2.0.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)
Verified my unit test and project bin/debug folder. Both have 5.2.2.0 version of DLL (which is expected). But not sure why my 5.2.0.0 version is being expected for running tests?
Verified that both machine have same IIS features installed (which should be completely irrelevant to my problem).
Made sure all DLL references are referenced through Solution Nuget package manager and hence share same set of DLLs in both machines (if somebody feels it should fix the problem, then I will try to cross verify again).
Remember my machine is able to run the Unit Tests successfully with same configuration. I assume any code change is not required. Could please anyone help me out to resolve this?
Thanks.

Could not load file or assembly Telerik.Web.UI in ASP.NET

I had telerik version 2012.2.607.35 When I update it to version 2015.2.623.45
I got this error:
Could not load file or assembly 'Telerik.Web.UI,
Version=2012.2.607.35, Culture=neutral, PublicKey
Token=121fae78165ba3d4' or one of its dependencies. The located
assembly's manifest definition does not match the assembly reference.
(Exception from HRESULT: 0x80131040)
I replaced all reference telerik to version 2015.2.623.45 in project references but the error exist yet.
While the assembly binding redirect is a way to solve this, as suggested by the answer, I would advise you to find the fully qualified assembly reference and make it non-fully qualified. It is likely to be a Register directive in the web.config or in a user control. Doing that will make future updates easier, otherwise you will get the same error the next time you upgrade unless you also update the bindingRedirect.
you should define assembly in web.config file
<dependentAssembly>
<assemblyIdentity name="YourDllFile" publicKeyToken="2780ccd10d57b246" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-YourDllFileVersion" newVersion="YourDllFileVersion" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Telerik.Web.UI" publicKeyToken="121fae78165ba3d4" />
<bindingRedirect oldVersion="1.0.0.0-2015.2.600.45" newVersion="2015.2.623.45" />
</dependentAssembly>
I had a similar problem. I checked the version of the .dll in the bin folder on my website and the version throwing the error. Since the code where the error was being thrown was not my own, I realised the version of the .dll was a dependency of 3rd party code. As it turns out the .dll in my bin had been overwritten with an older version.
Solution was to put the correct version of the .dll in the bin folder (since this was installed with the application initially) seems an update failed to install the newer version.

Running two versions of same Dll parallel in Asp.Net

I need to run two versions of same Dll to be run on my website simultaneously. So i have created a folder inside Bin and shown the second Dll path like this
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="AWSSDK" culture="neutral" publicKeyToken="9f476d3089b52be3"/>
<codeBase version="2.3.40.0" href="FILE://E://Live /bin/V-2/AWSSDK.dll"/>
</dependentAssembly>
</assemblyBinding>
and added my first dll as ordinary inside bin folder. But when I try to access any function inside my AWSSDK 2.3 Dll it throws an error like this
"Could not load file or assembly 'AWSSDK, Version=2.3.41.0, Culture=neutral, PublicKeyToken=9f476d3089b52be3' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"AWSSDK, Version=2.3.41.0, Culture=neutral, PublicKeyToken=9f476d3089b52be3"
Can anyone please point out what I am doing wrong here?
This error is because may be you have different versions of ddl but there namespace will be same, and you can reference only distinct namespaces in your project.
To overcome this,
Either create different namespaces for both versions or
use strong names for assemblies.

System.Net.Http.Formatting.dll causing issues with Newtonsoft.Json

My Windows service is in the same solution as a MVC project.
The MVC project uses a reference to SignalR Client which requires Newtonsoft.Json v6 +
the Windows service uses System.Net.Http.Formatting, which requires Newtonsoft.Json version 4.5.0.0.
I assumed this would not be a problem, as I could just use a binding redirect in my App.Config, however I get an error of
An unhandled exception of type 'System.IO.FileLoadException' occurred
in System.Net.Http.Formatting.dll
Additional information: Could not load file or assembly
'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral,
PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The
located assembly's manifest definition does not match the assembly
reference. (Exception from HRESULT: 0x80131040)
my app.config has the following:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
</dependentAssembly>
I added that myself, and it does not work, I have also tried uninstalling and re-installing Json.Net with the nuget package manager, to no avail
We faced same error and struggled to fix for few days. We finally we found this post on stack overflow Assembly reference cannot be resolved - dependentAssembly issue?
This made us realize to look into the version of System.Net.Http.Formatting being used and we found that our solution had been using multiple version of System.Net.Http.Formatting.dll and those each of them were referencing different version of Newtonsoft.Json.dll.
Removing references of older version of System.Net.Http.Formatting and adding references back, fixed the problem.
Hope that helps.
Does the assemblyBinding tag have proper xmlns schema? Check if the issue you are encountering is same as Assembly binding redirect does not work
#chintan123 actually pointed my in the right direction, turns out that despite the class library I was creating had a reference to both System.Net.Http.Formatting and also Newtonsoft.Json, only the former was being copied to the bin directory of the calling project that needed it.
Adding a reference to Newtonsoft.Json to the main calling project fixed the issue.

.NET loaded looking for another version of assembly when generate type's for WCF service

I got this error while trying to add service reference to my ASP.NET web application in Visual Studio 2013. I had reference to Microsoft.Owin.Security version 2.1.0.0 in my project. But I'm discouraged why he looking for 2.0.1.0 version?
Cannot import wsdl:portType Detail: An exception was thrown while
running a WSDL import extension:
System.ServiceModel.Description.DataContractSerializerMessageContractImporter
Error: Could not load file or assembly 'Microsoft.Owin.Security,
Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or
one of its dependencies. The system cannot find the file specified.
I did find and post a possible solution on Hélder Gonçalves' version of this question. Hopefully it will allow you to generate the service reference without redirecting to an older version of the Microsoft.Owin.Security assembly. Please let me know if this works for you.
Per Rizier123 suggestion, here's the text from my solution in full:
Had the exact same error verbatim and was able to resolve it by specifying which assemblies to reuse types from.
When adding the service reference, click the Advanced... button in the bottom left corner of the Add Service Reference window. On the Service Reference Settings screen that appears, in the Data Type section, under the Reuse Types in all referenced assemblies: check-box, select the Reuse types in specified reference assemblies radio button then check ONLY the assemblies that contain types used by the service. This should resolve the issue.
The assembly versions that are actually installed and referred varies due to various factors. Anyway you can redirect the binding to your desired versions. Refers this link
In your case, you have to add the following lines to the web.config under configuration
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="'Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-2.1.0.0" newVersion="2.0.1.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>

Categories