Could not load file or assembly 'System.Web.WebPages.Razor'?

27,342

One of your components depends on v2, hence the issue. You'll need to add binding redirect in web.config for the assembly to point to v3.

You can do it within nuget console with command

PM> Add-BindingRedirect

In case the tool does not pick up the version mismatch, you can add it manually to web.config:

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

One of the ways to find out what component depends on older version, you can use a dependency tracker tool. The one I've used in the past is called CheckAsm. The free version is good enough for one or two-times use.

Share:
27,342
Analytic Lunatic
Author by

Analytic Lunatic

Software Developer by Day, Web Designer by Night.

Updated on April 14, 2020

Comments

  • Analytic Lunatic
    Analytic Lunatic about 4 years

    I have an MVC5 Application that I am trying to publish to Microsoft Azure Webhosting. When I execute the application on my localhost, everything works as intended. However, when I publish to Azure and attempt to load the host domain, I receive the following error message:

    Could not load file or assembly 'System.Web.WebPages.Razor, Version=2.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).

    When I look into my Web.Config files, I have the following:

    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> </sectionGroup>

    If what I'm gathering from the error message is correct, either my application or some component is instead seeking Version 2.0.0.0 instead of 3.0.0.0, but I have no idea as to what/which may be causing the issue.

    Has anyone else encountered this when publishing to Azure?

    EDIT:

    As suggested by trailmax I performed Add-BindingRedirect in Package Manager Console, which resulted in the following:

    PM> Add-BindingRedirect
    Name                 OldVersion        NewVersion
    --------------       ------------      -------------
    System.Web.Mvc       0.0.0.0-5.1.0.0   5.1.0.0
    System.Web.WebPages  0.0.0.0-3.0.0.0   3.0.0.0
    

    I then checked localhost to verifying everything was still in working order (check) and tried publishing once more to Azure. Still the same issue :/

    Here's the full breakdown:

    Could not load file or assembly 'System.Web.WebPages.Razor, Version=2.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) 
      Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
     Exception Details: System.IO.FileLoadException: Could not load file or assembly 'System.Web.WebPages.Razor, Version=2.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)
    
    Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  
    
    Assembly Load Trace: The following information can be helpful to determine why the assembly 'System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' could not be loaded.
    
    WRN: Assembly binding logging is turned OFF. To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
    Note: There is some performance penalty associated with assembly bind failure logging. To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
    
    Stack Trace:  [FileLoadException: Could not load file or assembly 'System.Web.WebPages.Razor, Version=2.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)]
       Microsoft.Web.WebPages.OAuth.PreApplicationStartCode.Start() +0
    
    [InvalidOperationException: The pre-application start initialization method Start on type Microsoft.Web.WebPages.OAuth.PreApplicationStartCode threw an exception with the following error message: Could not load file or assembly 'System.Web.WebPages.Razor, Version=2.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).]
       System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection`1 methods, Func`1 setHostingEnvironmentCultures) +547
       System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +132
       System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +102
       System.Web.Compilation.BuildManager.ExecutePreAppStart() +153
       System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +521
    
    [HttpException (0x80004005): The pre-application start initialization method Start on type Microsoft.Web.WebPages.OAuth.PreApplicationStartCode threw an exception with the following error message: Could not load file or assembly 'System.Web.WebPages.Razor, Version=2.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).]
       System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9930568
       System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
       System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254
    

    EDIT2:

    Correction, the main root Web.config file was modified from Add-BindingRedirect. Still receiving the same issue on publish.

    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="0.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    
  • Analytic Lunatic
    Analytic Lunatic almost 10 years
    I tried the nuget command (see Edit), I'll now try the manual method.
  • Analytic Lunatic
    Analytic Lunatic almost 10 years
    I added the manual redirect as you suggested, placing the <runtime> and everything between just after the <configuration> in my file. Now when I run the application (localhost) it err's out on Parser Error Message: Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element.
  • trailmax
    trailmax almost 10 years
    that was not an exact code to insert, more like showing structure where you need to put binding redirect. Remove <configuration> around copy-pasted code, find <runtime> section of your web.config and put <assemblyBinding> section inside <runtime>. Unless you already have <assemblyBinding> - in that case only add <dependentAssembly> section.
  • Analytic Lunatic
    Analytic Lunatic almost 10 years
    Thanks. I've checked my web.config files and do not have a <runtime> section in any of them (why I went with the structure provided minus <configuration>..?
  • trailmax
    trailmax almost 10 years
    If you run Add-BindingRedirect it should have created runtime and assemblyBinding sections with redirected assemblies for System.Web.Mvc and System.Web.WebPages. Are you looking on the right file? Redirected assemblies should be only in web.config in the root of the site.
  • Analytic Lunatic
    Analytic Lunatic almost 10 years
    Correction, you are right, I was looking at the wrong file. The sections have been added (see Edit2), but still receiving the same issue with the published site.
  • trailmax
    trailmax almost 10 years
    Your edit shows binding redirect section, but that does not include System.Web.WebPages.Razor, is that how you have it or you just did not print here the added section?