Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly binding redirect not working

I am trying to deploy a test web app on Azure, but when I run the Azure emulator on my local machine I get this error from the Azure emulator console attached to my WebRole:

System.TypeLoadException: Unable to load the role entry point due to the following     exceptions:
-- System.IO.FileLoadException: Could not load file or assembly 'System.Web.Mvc, Version=3.0.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)
File name: 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

=== Pre-bind state information ===
LOG: User = COLLAB\mirko.lugano
LOG: DisplayName = System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
(Fully-specified)
LOG: Appbase = file:///C:/Code/Application/<MyWebProject>.Azure/csx/Debug/roles/<MyWebProject>/approot/bin
LOG: Initial PrivatePath = C:\Code\Application\<MyWebProject>.Azure\csx\Debug\roles\<MyWebProject>\approot\bin
Calling assembly : ActionMailer.Net.Mvc, Version=0.7.4.0, Culture=neutral, PublicKeyToken=e62db3114c02a1c2.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Code\Application\<MyWebProject>.Azure\csx\Debug\roles\<MyWebProject>\base\x64\WaIISHost.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: Attempting download of new URL file:///C:/Code/Application/<MyWebProject>.Azure/csx/Debug/roles/<MyWebProject>/approot/bin/System.Web.Mvc.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

---> System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
  at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
  at System.Reflection.RuntimeModule.GetTypes()
  at System.Reflection.Assembly.GetTypes()
  at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetRoleEntryPoint(Assembly entryPointAssembly)
  --- End of inner exception stack trace ---
  at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetRoleEntryPoint(Assembly entryPointAssembly)
  at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.CreateRoleEntryPoint(RoleType roleTypeEnum)
  at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.InitializeRoleInternal(RoleType roleTypeEnum)
[fabric] Role state Unknown

Everything had been working fine as long as I had MVC3 installed on my local machine (the MVC3 assembly was in GAC), but since I removed MVC3 (Azure has no MVC installed), I am getting this error. My web app has MVC4 regularly included and that works fine. I had then thought about assembly binding redirection, and I noticed that in my web.config file I already have:

  <runtime>    
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">      
    ...
    <dependentAssembly>        
      <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
    </dependentAssembly>
    ...
  </assemblyBinding>
</runtime>

which should be already correct, according to the documentation I have read around in internet. I have also tried setting the SpecificVersion property of my System.Web.Mvc.dll (version 4.0.0.0) to false but to no avail. Am I missing something? Shouldn't the calling assembly ActionMailer.Net.Mvc be automatically redirected to the correct version of the MVC assembly? Any ideas are greatly appreciated. Thanks.

like image 991
Mirko Lugano Avatar asked Mar 05 '13 14:03

Mirko Lugano


People also ask

What is assembly binding redirect?

Rely on automatic binding redirection This means that if two components reference different versions of the same strong-named assembly, the runtime automatically adds a binding redirection to the newer version of the assembly in the output app configuration (app. config) file.

Why does Nuget add binding redirects?

Binding redirects are added if your app or its components reference more than one version of the same assembly, even if you manually specify binding redirects in the configuration file for your app.


2 Answers

In Azure WebRoles, by default (in Full IIS Mode) the RoleEntryPoint gets walled off from the rest of the WebRole, and runs in a different process.

A side effect of this is that your RoleEntryPoint will not have access to your web.config.

  • Azure SDK 1.3 -1.7 will look in WaIISHost.exe.config

  • Azure SDK 1.8+ will look in the WebRoleProjectName.dll.config.

With the newest change to the SDK, you should be able to place an app.config in your project and your role entry point should then have access to it.

You can read more about this in this Microsoft blog post or in this Stackoverflow post

like image 111
Rune Vejen Petersen Avatar answered Oct 19 '22 17:10

Rune Vejen Petersen


Thank you Rune for the precious hint. That didn't solve my problem but it pointed me to the right direction, because I have the latest version of Azure SDK (1.8) and this WaIISHost.exe.config trick no longer works on the latest version of Azure. HERE is stated what I just said and the solution that worked for me, which is to rename the WaIISHost.exe.config file to MyWebAppName.dll.config (placing it at the same level of your web.config file in your web app and setting its Copy to output Directory property to 'Copy Always'. Of course this config file contains the binding redirect section as I described above.

like image 28
Mirko Lugano Avatar answered Oct 19 '22 17:10

Mirko Lugano