Why is assembly binding redirect not working in my

2020-08-09 08:27发布

问题:

I have a web site project that I run from Visual Studio using the built in development web server. The virtual path of the web site is set to /

The web.config contains a runtime element with

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="CMS.Controls" publicKeyToken="834b12a258f213f9" culture="neutral" />
      <bindingRedirect oldVersion="4.1.3518.21577" newVersion="4.1.3561.21846" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

I have already removed the xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0" attribute from the root configuration element.

Here is the error:

Could not load file or assembly 'CMS.Controls, Version=4.1.3518.21577, Culture=neutral, PublicKeyToken=834b12a258f213f9' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Here is the log of the binding:

The operation failed.
Bind result: hr = 0x80131040. No description available.
...
LOG: DisplayName = CMS.Controls, Version=4.1.3518.21577, Culture=neutral, PublicKeyToken=834b12a258f213f9
 (Fully-specified)
...
LOG: This bind starts in default load context.
LOG: Using application configuration file: D:\Project\WebSite\web.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
LOG: Post-policy reference: CMS.Controls, Version=4.1.3518.21577, Culture=neutral, PublicKeyToken=834b12a258f213f9
...
LOG: Assembly Name is: CMS.Controls, Version=4.1.3561.21846, Culture=neutral, PublicKeyToken=834b12a258f213f9
WRN: Comparing the assembly name resulted in the mismatch: Revision Number

Seems to me like it's ignoring my redirect. I've been looking at it for an hour, do I have a typo or something?

回答1:

i know it's been a while, but maybe this can help someone sometime...

We got the exact same problem on some project, it was a webapplication and not a website, but as the problem concern referenced assemblies i don't think the difference of project type is relevant (i may be wrong)

Let's say we have the following assemblies:

  • WebApplicationAssembly

    1. CMSControlAssembly
    2. UserManagementAssembly

WebApplicationAssembly is referencing CMSControlAssembly and UserManagementAssembly.

So we tried to do an assemblybinding on CMSControlAssembly, with as much success as you do.

After some digging the lights came up:

the thing is that we used the webcontrols inside CMSControlAssembly directly within our webapplication. (the assemblybinding was set with this in mind)

But CMSControlAssembly was also referenced by UserManagementAssembly, and this was the cause of our problem.

UserManagementAssembly was compiled with a lower version of CMSControlAssembly than the one used by the webapplication.

This lower version of the assembly was nowhere to be found by the webapplication, as the only version provided was the one targeted by the assemblybinding.

So actually the error is not showing a non functional assemblybinding, but the assembly missing for UserManagementAssembly.



回答2:

Is this a 64bit/32bit dll issue, sometimes there are different builds of a dll for different systems. Even if you are running a 64bit version of Windows the development web server runs as a 32bit application.

You can switch to a 64bit dev server if you switch to CassinDev Server. It works just the same.



回答3:

The only reason that can explain an error like this is that new version is not deployed on the GAC (in the windows assembly folder).

Important: Not deployed applies as well if the public key does not match between versions for example version 1 public key was abcd but new version uses efgh. Make sure public matches and that thew version is on the GAC.



回答4:

I got a similar problem recently with using two versions so i did cleaning the solution and Rebuild again it worked sometimes. Also if clean did not clear the files from bin folder, i deleted the files manually and build again. Depending on the framework version we need to clear the files, for .net 4 here is the folder to find the temporary files where the old version is receding C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files