System.Web.Razor.dll未复制到bin文件夹



问题

我们有一个ASP.Net 5.2.3项目,正试图升级到.Net 4.6。

运行时,我们会收到错误消息

无法加载文件或程序集"System.Web.Razor,Version=3.0.0.0,Culture=neutral,PublicKeyToken=31bf3856a364e35"或其依赖项之一。系统找不到指定的文件。

请注意,我们从NuGet安装了Microsoft.AspNet.Razor 3.2.3版本,尽管错误消息指的是3.0.0.0版本。

破解

现在,如果我手动复制

软件包\Microsoft.AspNet.Razor.3.2\lib\net45\System.Web.Razor.dll

WebProject\bin

这个项目运行得很好。

GAC

注意中有两个条目

C: \Windows\Microsoft.NET\程序集\GAC_MSIL\System.Web.Razor

v4.0_1.0.0.0__31bf3856ad64e35

v4.0_2.0.0.0__31bf3856ad64e35

我知道GAC中存在的DLL会忽略CopyLocal=True。然而,我不明白为什么该项目既无法解析来自GAC的引用,又不愿意将使用NuGet引用的版本复制到bin文件夹。

web.config

我们在web.config中有以下绑定重定向,这些重定向可能是由NuGet安装程序放置的

  <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>

我们还尝试将newVersion="3.2.3.0"设置为与NuGet包管理器中显示的版本相匹配。当我们将两者都更改为时

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

我们得到一个略有不同的错误

无法加载文件或程序集"System.Web.WebPages.Razor"或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。(HRESULT异常:0x80131040)

问题

应该如何设置项目引用以解决此问题?

您需要将程序集重定向添加到*.config文件中。打开Package Manager控制台并输入以下命令:Add-BindingRedirect [-ProjectName]

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

尝试右键单击引用列表中的引用,并将"Copy Local"设置为"True"。

正如我所怀疑的,您的配置将其强制为3.0.0.0。试试这个:

<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>

最新更新