关于c#:dotnet发布配置文件CLI与Visual Studio的行为不同

 2021-04-26 

dotnet publish profile CLI vs Visual Studio behave different

我有一个dotnet core 2.1 Web API项目,该项目的发布配置文件到一个文件夹,最近我打算像dotnet publish文档中一样复制到IIS。

我用VS创建了发布配置文件,结果文件为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <PublishProvider>FileSystem</PublishProvider>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <ProjectGuid>b3155c62-817f-4eba-8856-e5941137f4ed</ProjectGuid>
    <SelfContained>true</SelfContained>
    <_IsPortable>false</_IsPortable>
    <publishUrl>bin\\publish\\</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles>
  </PropertyGroup>
</Project>

如果我从VS运行配置文件,它将创建一个bin\\publish文件夹,可以将其复制到IIS。

如果在更新到最新的VS之后对dotnet publish /p:PublishProfile=FolderProfile进行相同操作(如CI服务器中的操作),则会出现以下错误

C:\\Program Files\\dotnet\\sdk\\2.1.400\\Sdks\\Microsoft.NET.Sdk\\targets\\Microsoft.NET.RuntimeIdentifierInference.targets(122,5): error NETSDK1067: Self-contained applications are required to use the application host. Either set SelfContained to false or set UseAppHost to true. [C:\\Users\\Guillem.Sola\\source\
epos\\ASG.DHW.HealthCheck\\ASG.DHW.HealthCheck.API\\ASG.DHW.HealthCheck.API.csproj]

我可以在cmd中实现类似的操作

dotnet publish .\\HealthCheck.API\\HealthCheck.API.csproj -o .\\bin\\publish -r win-x64 -c Release

这是怎么回事,为什么从CLI调用时配置文件的行为不相同?


我认为您(已更新)问题的关键可能在错误消息的此部分中:

Self-contained applications are required to use the application host. Either set SelfContained to false or set UseAppHost to true.

行为上的差异可以很容易地用许多事情来解释,例如,各种VS条件带来的MSBuild目标文件。

由于您不想使用AppHost,因此需要将<SelfContained>true</SelfContained>更改为<SelfContained>false</SelfContained>。您还可以考虑添加一个显式的<UseAppHost>false</UseAppHost>-这可能有助于减轻VS和CI构建之间的差异。

打开/打开MSBuild详细信息是获取更多数据的好方法,以帮助您了解操作为何具有可观察到的结果。