FileLoadException Occured for System.Web.Http - c#

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.

Related

Version Independent reference in C# Class Library

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.

Could not load file or assembly 'Microsoft.WindowsAzure.Storage' or one of its dependencies. When running chat bot C#

I am trying to run my chat bot after I made some modifications within Visual Studio. My code works correctly when run locally with the bot framework emulator, but when published to my chat bot stored on Azure I get the error:
Could not load file or assembly 'Microsoft.WindowsAzure.Storage' or
one of its dependencies. The located assembly's manifest definition
does not match the assembly reference. (Exception from HRESULT:
0x80131040)
Any help or insight into this problem would be much appriciated!
Open nuget console and paste in to install the missing package:
Install-Package WindowsAzure.Storage -Version 9.3.1
Remove this from the web config file.
<dependentAssembly> <assemblyIdentity name="Microsoft.WindowsAzure.Storage" publicKeyToken="31bf3856ad364e35" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-9.3.1.0" newVersion="9.3.1.0" /> </dependentAssembly>
Remove these lines from the web config file and then rebuild your application.

Application trying to reference incorrect assembly version

I have a application running on a windows server 2012 that was using Telerik.Web.UI.dll version 2014.1.403.45.
During the latest deployment the Telerik dll under /bin folder got updated with 2015.1.401.40.
Because of new dll some functionality stopped working.
When i replace the dll on server with older version i get following error
Parser Error Message: Could not load file or assembly 'Telerik.Web.UI, Version=2015.1.401.40, Culture=neutral, PublicKeyToken=121fae78165ba3d4' or one of its dependencies.
The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
I also tried to assembly binding
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Telerik.Web.UI" publicKeyToken="121fae78165ba3d4" culture="neutral"/>
<bindingRedirect oldVersion="2015.1.401.40" newVersion="2014.1.403.45"/>
<assemblyIdentity name="Microsoft.WindowsAzure.Storage" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-2.1.0.4" newVersion="2.1.0.4"/>
</dependentAssembly>
</assemblyBinding>
I am still getiing the same error.
Is there any way to handle this without deplying the entire code again?
i got this type of issue, but in different case(in Unit Testing). for that I cleared the Bin. then did a build. then issue resolved for me.
Maybe there is a way, but I would redeploy. Build it again with the reference to the older version. That's the safest way and the best way to make sure all references point to the same assembly.
To be absolutely sure about the right references, set the dll to copy to local is true, and copy and replace it with the one on your server.

How to add dependent assembly without editing webConfig?

So according to my boss we are not allowed to edit the webConfig (I know weird right?). I've added nuGet packages for Microsoft.Owin and other dependent packages. These packages made changes to the webConfig by dependent assemblies to a runtime node.
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
...etc...
My question is can I remove the runtime node and its contents but still have the same functionality without removing or changing code? If I remove the node now I get the following error:
Could not load file or assembly 'Microsoft.Owin, Version=2.1.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)
Whenever you see a bindingRedirect element in your application config file, this means that you have some inconsistency between the versions of the assemblies you are using. The NuGet system automatically detects this inconsistency and adds the binding redirect to ensure that at least you don't get compile time errors. Of course at runtime you might have problems with it because the assembly that depends on the older version of Microsoft.Owin might not work as expected with the one you are redirecting to: especially if there are some breaking changes and it attempts to call a method that no longer exists. The sole fact that you got a binding redirect added in your config file when installing the NuGet means that you might a problem and this problem might manifest only under certain specific circumstances at runtime.
So basically I would start by looking at all dependent assemblies that are references in this project along with their dependencies and try to see where this inconsistency of the versions come from. Then try to eliminate it by finding a newer version of this assembly that works with the latest Microsoft.Owin.

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.

Categories