MvcBuildViews 不适用于 x64 项目

问题描述

在 VS2017 ASP.NET MVC 项目中,我注意到即使我将 MvcBuildViews 设置为 true,我的视图也没有在构建/编译时进行语法检查。该项目现在在 MVC 5 上,但已从某个早期版本升级。它也开始设置为 Anycpu,但现在设置为 x64(有时我们需要内存中的大量数据,我们总是部署到 x64,所以...)。

我已经检查过 .csproj 包含在不同地方提到的构建目标,包括 SO 答案中的几个。我尝试了一些变体,但都没有奏效。

这是我开始调查时出现但在 .csproj 中注释掉的:

<Target Name="BuildViews" Condition="'$(MvcBuildViews)'=='true'" AfterTargets="Build">
  <Message Importance="normal" Text="Precompiling views" />
  <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>

如果我取消注释,我会收到一个构建错误(我已经从瑞典语手动翻译了它):

无法读取文件或程序集 MyProjectName 或其依赖项之一。尝试读取格式无效的程序。

文件C:\...MyProjectPath...\ASPNETCOMPILER

建议使用 here 指定 x64 ASP.NET 编译器: 在 <MvcBuildViews> 标签下方添加

<AspNetToolPath Condition="'$(Platform)' == 'x64'">$(windir)\Microsoft.NET\Framework64\v4.0.30319</AspNetToolPath>

并将属性 ToolPath="$(AspNetToolPath)" 添加到目标的 <AspNetCompiler.../> 标签

这样做会导致另一个错误(我再次从瑞典语翻译过来):

无法读取文件或程序集 System.Runtime.InteropServices.Runtimeinformation,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a 或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。 (来自 HRESULT 的异常:0x80131040)

文件C:\temp\global.asax

有问题的程序集是通过我们使用 Stripe 卡支付 NuGet 包拉入项目的,该包是在项目从 Anycpu 更改为 x64 后添加的。 System.Runtime.InteropServices.Runtimeinformation 软件包安装在 4.3.0 版中。 web.config 包含此绑定重定向(为什么版本号与 4.3.0 不匹配?):

<dependentAssembly>
  <assemblyIdentity name="System.Runtime.InteropServices.Runtimeinformation" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0"/>
</dependentAssembly>

此时,我没有想法,下一步要尝试什么。有人可以帮忙吗?

解决方法

尝试将该 dll 注册到 GAC:

试试这些:

1)工具-->Nuget Package Manager-->Package Manager Consoleupdate-package -reinstall /strong>

2) 以管理员身份打开 VS 的开发人员命令提示符,然后输入:

cd C:\Users\xxx\.nuget\packages\system.runtime.interopservices.runtimeinformation\4.3.0\lib\net45

gacutil /i System.Runtime.InteropServices.RuntimeInformation.dll

此外,您还可以下载 system.runtime.interopservices.runtimeinformation nuget 软件包版本 4.0.0

之后,在C:\Users\xxx\.nuget\packages\system.runtime.interopservices.runtimeinformation\4.0.0\lib\net45下注册那个dll。

此外,还有a similar issue