13

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
Mirko Lugano
  • 975
  • 1
  • 11
  • 26
  • **BindingRedirect** should work - is this `bindingRedirect` in the [`WaIISHost.exe.config` file](http://stackoverflow.com/a/14072929/175679)? It sounds like the redirect isn't there. Share the contents of `C:\Code\Application\.Azure\csx\Debug\roles\\base\x64\WaIISHost.exe.Config` – SliverNinja - MSFT Mar 05 '13 at 14:56
  • 1
    Thank you SilverNinja, this is the content of the file you requested, and indeed the binding redirect isn't there. ` ` How can I add it so that it is included when I deploy the WebRole? Of course I cannot add it manually to the file.... – Mirko Lugano Mar 05 '13 at 15:11

3 Answers3

11

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

Community
  • 1
  • 1
Rune Vejen Petersen
  • 3,201
  • 2
  • 30
  • 46
  • I followed the pre-build action method from this MS blog post. http://blogs.msdn.com/b/friis/archive/2014/05/15/webrole-entry-point-and-config-file.aspx – praneetloke Jun 03 '15 at 20:46
5

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.

Mirko Lugano
  • 975
  • 1
  • 11
  • 26
  • You're right - they changed it again :) After doing a little bit of research, I found that you can now just add an app.config file to your solution which can be read by the RoleEntryPoint. I've edited my answer to reflect this – Rune Vejen Petersen Mar 05 '13 at 16:57
  • another precisation: this is valid if your Azure web app runs in full IIS mode. Otherwise, if it runs in Hosted Web Core mode it 'should' work without this hack (I haven't tried it though). See [HERE](http://blogs.msdn.com/b/windowsazure/archive/2010/12/02/new-full-iis-capabilities-differences-from-hosted-web-core.aspx) – Mirko Lugano Mar 06 '13 at 08:14
  • whoops, sorry Rune, I found it by myself a second time after a little bit of extra research. Thx very much. :) – Mirko Lugano Mar 07 '13 at 15:41
2

As of today with the latest SDK a WebRoleProjectName.dll.config will be automatically generated for you with the contents of the Web.config and copied to the output directory.

However this won't be automatically be included in the deployment package! For this to happen you have to employ a kind of hackish solution: include the generated file in the project by selecting "show all files", then including just this file. The resulting change in the csproj should look like this (not, don't select it to be "Copy always"!):

<Content Include="bin\WebRoleProjectName.dll.config" />
Piedone
  • 2,693
  • 2
  • 24
  • 43
  • I'll post my comment to this answer as well. See this: http://blogs.msdn.com/b/friis/archive/2014/05/15/webrole-entry-point-and-config-file.aspx. I used the pre-build action to generate the .dll.config file and then added it manually to the project from the bin directory. – praneetloke Jun 03 '15 at 20:47