none
TypeLoadException even with a bindingRedirect

    Question

  • During RoleEnvironment.GetRoleEntryPoint I am getting a TypeLoadException as the the runtime looks for System.Web.Mvc 2.0.0.0.

    This is a MVC 3 web role and in the web.config I have a bindingRedirect that should be redirecting all assemblies to version 3.

     

    The facts:

    1. MVC 3 web role with MVC dlls all present and correct.

    2. MvcContrib dll that has a reference to MVC 2.

    3. Web.config with the following redirect:

     

      <dependentAssembly>
         <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
       </dependentAssembly>
    

     

    During deploment the role reports itself as unhealthy and intellitrace reports a TypeLoadException.

    If I RDP to the instance and browse using the on machine browser, the web role is browsable which means the web site is running, but that the OnStart method has NOT run due to the exception, so all my trace diangnostics are not running as these are set in the OnStart event.

     

    Can anyone tell me why it is trying to load the old assembly when I have a bindingRedirect configured? Also does anyone have a suggestion for fixing this.

     

    Friday, June 10, 2011 9:25 AM

Answers

  • Hi Simon,

    From you descritpion, you must be using a web role in Full IIS mode and the WebRole role does not pick up the settings in web.config file. This happens bacause in Full IIS mode, the web site will be hosted in IIS and will run in w3wp.exe process, but WebRole class will be run in WaIISHost.exe process. Thus, the web site use web.config as configuration file but WebRole class does not, though they belong to the same web role project. Please see http://blogs.msdn.com/b/windowsazure/archive/2010/12/02/new-full-iis-capabilities-differences-from-hosted-web-core.aspx for more information.

    There are two workarounds for resolving the configuration issue:

    1. Create a WaIISHost.exe.config file in the web role project and set it as "Copy if newer". Then we can write settings in this file and it will be used by WebRole class.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <startup useLegacyV2RuntimeActivationPolicy="true">
            <supportedRuntime version="v4.0" />
        </startup>
        <runtime>
            <NetFx40_LegacySecurityPolicy enabled="false" />
            <assemblyBinding>
                <dependentAssembly>
                    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
                    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
                </dependentAssembly>
            </assemblyBinding>
        </runtime>
    </configuration>

    2. Switch back to the Hosted Web Core mode by commenting out sites element so that both the web site and WebRole class use the web.config as configuration file. Usig this way, you will lose the benefit of using Full IIS mode, such as hosing multiple web sites in a web role etc.

    <!--<Sites>
        <Site name="Web">
        <Bindings>
            <Binding name="Endpoint1" endpointName="Endpoint1" />
        </Bindings>
        </Site>
    </Sites>-->

    If both these do not work for you, please let me know then we can consider configureing a Web Role for Multiple Web Sites so that we can define the MVC web site and WebRole class in separated projects, thus WebRole class does not depends on System.Web.Mvc assembly.

    Thanks,


    Wengchao Zeng
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
    • Marked as answer by Wenchao Zeng Monday, June 20, 2011 8:54 AM
    Monday, June 13, 2011 6:05 AM

All replies

  • Hi Simon,

    From you descritpion, you must be using a web role in Full IIS mode and the WebRole role does not pick up the settings in web.config file. This happens bacause in Full IIS mode, the web site will be hosted in IIS and will run in w3wp.exe process, but WebRole class will be run in WaIISHost.exe process. Thus, the web site use web.config as configuration file but WebRole class does not, though they belong to the same web role project. Please see http://blogs.msdn.com/b/windowsazure/archive/2010/12/02/new-full-iis-capabilities-differences-from-hosted-web-core.aspx for more information.

    There are two workarounds for resolving the configuration issue:

    1. Create a WaIISHost.exe.config file in the web role project and set it as "Copy if newer". Then we can write settings in this file and it will be used by WebRole class.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <startup useLegacyV2RuntimeActivationPolicy="true">
            <supportedRuntime version="v4.0" />
        </startup>
        <runtime>
            <NetFx40_LegacySecurityPolicy enabled="false" />
            <assemblyBinding>
                <dependentAssembly>
                    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
                    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
                </dependentAssembly>
            </assemblyBinding>
        </runtime>
    </configuration>

    2. Switch back to the Hosted Web Core mode by commenting out sites element so that both the web site and WebRole class use the web.config as configuration file. Usig this way, you will lose the benefit of using Full IIS mode, such as hosing multiple web sites in a web role etc.

    <!--<Sites>
        <Site name="Web">
        <Bindings>
            <Binding name="Endpoint1" endpointName="Endpoint1" />
        </Bindings>
        </Site>
    </Sites>-->

    If both these do not work for you, please let me know then we can consider configureing a Web Role for Multiple Web Sites so that we can define the MVC web site and WebRole class in separated projects, thus WebRole class does not depends on System.Web.Mvc assembly.

    Thanks,


    Wengchao Zeng
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
    • Marked as answer by Wenchao Zeng Monday, June 20, 2011 8:54 AM
    Monday, June 13, 2011 6:05 AM
  • Just in case someone else stumbles in here like I did, this solution doesn't work anymore as written in Azure SDK v1.8+. You now have to create a config file called <YourDLLName>.dll.config. 

    More discussion here: http://blog.fullscale180.com/post/2012/11/08/WaIISHostexeconfig-in-Web-Roles-does-not-work-in-Windows-Azure-SDK-18

    Wednesday, October 16, 2013 2:46 AM