我的解决方案包含一些 .net 项目,其中之一是我正在尝试发布的 ASP.NET MVC 项目。所有配置均已正确设置,x32 和 x64,没有设置为 AnyCPU。
问题:
如果我尝试将项目发布为 32 位,一切都很好,但尝试以 64 位模式发布会失败并出现错误:
Could not load file or assembly "ProjectA" or one of its dependencies.
An attempt was made to load a program with an incorrect format.
我尝试过并注意到的:
自 VS 2013 起,MSbuild 成为 VS 的一部分,而不是像以前那样属于 .NET Framework。如果我只是在 x64 模式下构建解决方案,则 32 位 msbuild
"C:\Program Files (x86)\MSBuild\12.0\Bin\MSBuild.exe"
首先运行,并启动 64 位 msbuild "C:\Program Files (x86)\MSBuild\12.0\Bin\amd64\MSBuild.exe"
因此,无需发布的正常构建就可以正常工作。
但是,如果我选择
publish
,则 32 位 MSbuild 将首先运行,然后启动 32 位 aspnet_compiler c:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_compiler.exe
,而不是 64 位编译器,这会导致我上面提到的错误。
到目前为止我发现的唯一解决方法是更换
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_compiler.exe"
64位的
"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_compiler.exe"
问题:
对于我的问题有更好的(合法的)解决方案吗?这看起来像是 VS 中的一个错误
将此行添加到您要定位的构建配置的
PropertyGroup
节点内的 .csproj 文件中(或使用没有目标的 ProperyGroup
来定位所有发布模式)。
<AspnetCompilerPath>$(windir)\Microsoft.NET\Framework64\v4.0.30319</AspnetCompilerPath>
编译器随后使用 64 位版本。对我来说,我添加此行的节点如下:
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<AspnetCompilerPath>$(windir)\Microsoft.NET\Framework64\v4.0.30319</AspnetCompilerPath>
</PropertyGroup>
将此行添加到发布配置的 PropertyGroup 节点内的 .pubxml 文件 (Tree Solution\Project\Properties\PublishProfiles
例如:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!-- Add the line below... -->
<AspnetCompilerPath>$(windir)\Microsoft.NET\Framework64\v4.0.30319</AspnetCompilerPath>
<WebPublishMethod>FileSystem</WebPublishMethod>
<LastUsedBuildConfiguration>Debug</LastUsedBuildConfiguration>
<LastUsedPlatform>Any CPU</LastUsedPlatform>
<SiteUrlToLaunchAfterPublish />
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
<ExcludeApp_Data>False</ExcludeApp_Data>
<publishUrl>C:\Pub\FTS_Service</publishUrl>
<DeleteExistingFiles>True</DeleteExistingFiles>
<PrecompileBeforePublish>True</PrecompileBeforePublish>
<EnableUpdateable>True</EnableUpdateable>
<DebugSymbols>False</DebugSymbols>
<WDPMergeOption>DonotMerge</WDPMergeOption>
</PropertyGroup>
</Project>
我也有同样的问题。
您可以在开始发布之前创建 BAT 文件来替换 EXE。 或者 或者您可以编写一个 BAT 直接调用 aspnet_compiler.exe 并在没有 UI 的情况下进行发布 :-)