Eric J.
Eric J.

Reputation: 150108

System.Web.Razor.dll not copied to bin folder

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?

Upvotes: 3

Views: 3167

Answers (3)

mxmissile
mxmissile

Reputation: 11673

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

Upvotes: 1

mjw
mjw

Reputation: 1206

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>

Upvotes: 0

swinkel
swinkel

Reputation: 220

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

Upvotes: 0

Related Questions