web.config bindingRedirect not working

iqworks Information Quality Works 276 Reputation points
2024-05-11T22:47:30.2733333+00:00

Hi, I have two projects. I have a MVC web application called MBSAnalysisMVCWebApp. I also have a .net framework MVC project called Mbsa.

I found this link:
https://stackoverflow.com/questions/15056499/what-is-the-meaning-reason-for-the-generated-entries-in-web-configconfiguration

Does this mean if I have a web project that depends on a class library and that class library has a reference to an older version of the assembly which has a a bindingRedirect, that the code will execute as if it were compiled against the newer version?

You have it right (I would just say "...the code will execute as if it were referencing the newer version"), see [http://msdn.microsoft.com/en-us/library/7wd6ex19%28v=vs.110%29.aspx]

"When you build a .NET Framework application against a specific version of a strong-named assembly, the application uses that version of the assembly at run time. However, sometimes you might want the application to run against a newer version of an assembly."

answered Feb 24, 2013 at 21:13

  • Yep, you guys are right, I used it for the sort of same purpose; for example say I have a library called libA.dll and another library called libB.dll. My version of libA.dll is compiled against version 1.0 of libB.dll. I have updated my application with a newer version of libB.dll (say 1.5) and libA.dll is no longer working. I would use this feature to instruct the runtime to get libA.dll to work with the newer version of libB.dll (the other, arguably better option is, if I have source code of libA.dll to compile it against the newer version of libB.dll). **– Chintana Meegamarachchi Feb 25, 2013 at 3:14

Here are y two projects : Mbsa.dll and then MBSAnalysisMVCWebApp.

Mbsa.dll

User's image

MBSAnalysisMVCWebApp.

User's image

I want to access a controller method in MBSAnalysisMVCWebApp, from an Mbsa controller method.

 MBSAnalysisMVCWebApp has a system.web.mvc of 5.7.2.0 Mbsa a system.web.mvc of 4.0.0.1. 

I tried to change the Mbsa 4.0.0.1 to 5.7.2.0 using this in my web.config: 

<dependentAssembly>

   <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />

   <bindingRedirect oldVersion="0.0.0.0-4.0.0.1" newVersion="5.7.2.0" />

 </dependentAssembly>

 The following, is the code from within Mbsa that I used to try to connect to the controller method in MBSAnalysisMVCWebApp: 

MBSAnalysisMVCWebApp.Controllers.MBSAnalysisMainController iwebapp =

new MBSAnalysisMVCWebApp.Controllers.MBSAnalysisMainController();

iwebapp.DisplayLoggingMaint();

But I get this error :

Assembly
'MBSAnalysisMVCWebApp' with identity 'MBSAnalysisMVCWebApp,
Version=2024.5.11.25, Culture=neutral, PublicKeyToken=null' uses
'System.Web.Mvc, Version=5.2.7.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35' which has a higher version than referenced
assembly 'System.Web.Mvc' with identity 'System.Web.Mvc, Version=4.0.0.1,
Culture=neutral, PublicKeyToken=31bf3856ad364e35'
Mbsa Not sure of what I am doing wrong? 

Thanks for any suggestions or advice

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,315 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 49,246 Reputation points
    2024-05-16T19:18:01.57+00:00

    You cannot use any Microsoft.AspNet.* packages greater than 4.* in MVC 4. It will not work. The runtimes are different. Loading a v5.* version of MVC into the MVC 4 runtime will have undefined behavior. Until you upgrade to MVC 5 you will need to stick with MVC 4 binaries including all Microsoft.AspNet.* packages. There is no workaround for this.

    A binding redirect is strictly for the assembly loader (fusion) to use. When you reference assemblies that are strongly named then your binary is hard coded to use only that version. If any other version is found then it will fail to load. This is a compatibility and security feature and only applies to strongly named assemblies. In some cases you need to use a newer version of an assembly but you cannot update the dependencies at build time. In this case a binding redirect tells the runtime that any request for versions (0.0.0.0-w.x.y.z) will instead use version w.x.y.z. The upper end is always the same as the version you want to load. Hence if your code previously targeted v1.2.3.4 but you have applied a patch to the dependency that now is v1.3.0.0 AND there is no compatibility issues then you can put in a binding redirect telling the runtime to load the v1.3.0.0 version whenever you request any version between 0.0.0.0 and 1.3.0.0.

    The use case for this is very specific and, if misused, can cause runtime errors. The use case is when your project X relies on some assembly A that itself relies on some assembly B. Assembly B, through whatever scenario, has a newer version that you need to use. You update project X to depend on the newest version of assembly B. However assembly A (that you also rely on) will fail because it expects the old version. Adding a binding redirect tells the runtime that when assembly A looks for the older version then go ahead and load the newer version anyway. It is up to you to confirm that the versions are compatible and that it won't break the code. The runtime just loads what you tell it to.

    Now back to what you're trying to do. If you have an assembly that relies on MVC 5 AND you are targeting MVC 4 then no amount of binding redirect is going to help here. The assemblies are a major version different AND they are not compatible. So while a binding redirect might allow the assembly to load (unlikely since it also has dependencies that would need redirects) they are not compatible and therefore you'll get runtime errors.

    The only workaround is to either upgrade your solution to MVC 5, in which case all the problems go away, or host 2 separate web apps (one MVC 4 and one MVC 5). The MVC 4 app, when it needs to use functionality defined in MVC 5) would need to proxy calls over to the other app. You cannot load MVC 5 binaries into an MVC 4 runtime.