3

Problem

We have an ASP.Net 5.2.3 project that we are attempting to upgrade to .NET 4.6.

When running it, we get the error message

Could not load file or assembly 'System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

Note that we installed Microsoft.AspNet.Razor version 3.2.3 from NuGet, though the error message refers to version 3.0.0.0.

Hack

Now if I manually copy

packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll

to

TheWebProject\bin

the project runs just fine.

GAC

Note that there are two entries in

C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Web.Razor

v4.0_1.0.0.0__31bf3856ad364e35

v4.0_2.0.0.0__31bf3856ad364e35

I'm aware that DLLs that are present in the GAC ignore CopyLocal=True. However, I don't understand why the project is both unable to resolve the reference from the GAC, and unwilling to copy the version referenced using NuGet to the bin folder.

web.config

We have the following binding redirects in web.config that were presumably placed there by the NuGet installer

  <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>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Razor" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>

We also tried setting newVersion="3.2.3.0" to match the version as it appears in the NuGet package manager. When we change both of those to

<bindingRedirect oldVersion="0.0.0.0-3.2.3.0" newVersion="3.2.3.0" />

we get a slightly different error

Could not load file or assembly 'System.Web.WebPages.Razor' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Question

How should the project references be setup to resolve this issue?

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553

3 Answers3

1

You need to add assembly redirects to your *.config file. Open up the Package Manager Console and enter this command: Add-BindingRedirect [-ProjectName]

reference: http://weblog.west-wind.com/posts/2014/Nov/29/Updating-Assembly-Redirects-with-NuGet and https://docs.nuget.org/consume/package-manager-console-powershell-reference

mxmissile
  • 11,464
  • 3
  • 53
  • 79
  • That's a cool command, I did not know about it. However, after running it, it made the same change to the binding redirect that we tried by hand (updated it to 3.2.3). – Eric J. Dec 07 '15 at 18:37
  • Could there be a *seperate* dependent assembly that has been compiled against an older version of System.Web.Razor? – mxmissile Dec 07 '15 at 18:39
  • Exactly what one of the team members said when we first started digging into this :-) – Eric J. Dec 07 '15 at 18:45
  • `Could there be a seperate dependent assembly` we checked for that pretty carefully, but good thought. – Eric J. Dec 07 '15 at 18:46
0

Try right clicking on the reference in the reference list and set "Copy Local" to "True".

swinkel
  • 220
  • 2
  • 15
0

As I suspected, your config is forcing it to 3.0.0.0. Try this:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        <bindingRedirect oldVersion="3.0.0.0" newVersion="3.2.3.0" />
      </dependentAssembly>      
    </assemblyBinding>
  </runtime>
mjw
  • 1,196
  • 1
  • 12
  • 19
  • We already tried that. I updated the question to make that clearer, and showed the error we got. – Eric J. Dec 07 '15 at 18:35