c# - Azure DevOps Pipeline 不从 Net Core 3.1 应用程序生成单个文件 exe

标签 c# asp.net-core .net-core azure-devops yaml

我想自动化我的部署。到目前为止,我一直通过 Visual Studio 2019 手动发布我的 Net Core 3.1 解决方案,没有出现任何问题。然而,当我想使用 Azure DevOps Pipeline 时,即使我没有收到任何错误,我总是会得到数百个 .dll 文件,而不是一个 .exe 文件

这是我的 Visual Studio 配置,可以按预期工作:

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <DeleteExistingFiles>True</DeleteExistingFiles>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <PublishProvider>FileSystem</PublishProvider>
    <PublishUrl>C:\Users\Karel Křesťan\Desktop\apm-local</PublishUrl>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <SiteUrlToLaunchAfterPublish />
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PublishSingleFile>True</PublishSingleFile>
    <ProjectGuid>870788a8-f14a-4683-8395-6048e2c9aa1e</ProjectGuid>
    <SelfContained>true</SelfContained>
  </PropertyGroup>
</Project>

如您所见,它是单个文件且独立的

这是我在 DevOps 上的 yaml 文件:

    trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  imageName: 'fine-project-manager'

steps:
- task: UseDotNet@2
  inputs:
    packageType: 'sdk'
    version: '3.1.107'

- task: MSBuild@1
  inputs:
    solution: '**/*.sln'
    msbuildArguments: '/t:restore;rebuild;publish /p:DeployOnBuild=True /p:DeployDefaultTarget=WebPublish /p:WebPublishMethod=FileSystem /p:SelfContained=true /p:Platform="Any CPU" /p:Configuration=Release /p:RuntimeIdentifier=win-x64'
    msbuildVersion: latest

- task: MSBuild@1
  inputs:
    solution: '**/*.sln'
    msbuildArguments: '/T:"ApmBackend" /t:publish /p:DeployOnBuild=True /p:DeployDefaultTarget=WebPublish /p:WebPublishMethod=FileSystem /p:SelfContained=true /p:Platform="Any CPU" /p:Configuration=Release /p:PackageAsSingleFile=true /p:RuntimeIdentifier=win-x64 /p:OutputPath=$(Build.ArtifactStagingDirectory)'
    msbuildVersion: latest

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'publish'
    publishLocation: 'Container'

管道工作并成功发布应用程序,但是,它不是单个文件。知道什么可能会导致麻烦吗?

有关管道的小注释: 我正在使用两个 MSBuild,因为我无法找到更好的解决方法来解决从具有多个项目的解决方案发布单个文件应用程序的问题。

当我尝试发布解决方案时,每个 .csproj 文件出现以下错误:

error NETSDK1099: Publishing to a single-file is only supported for executable applications.

这就是为什么我首先发布整个解决方案,然后仅发布可执行的 ApmBackend.csproj。不是很干净,但它可以工作,我不知道任何其他修复。

最佳答案

对于将来尝试在 azure 上生成单个 exe 的人来说,这是真正有效的 yaml(我确信有一个更干净、更简单的解决方案,但我找不到它):

# ASP.NET Core (.NET Framework)
# Build and test ASP.NET Core projects targeting the full .NET Framework.
# Add steps that publish symbols, save build artifacts, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/dotnet-core

trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  imageName: 'fine-project-manager'

steps:
- task: UseDotNet@2
  inputs:
    packageType: 'sdk'
    version: '3.1.107'

- task: MSBuild@1
  inputs:
    solution: '**/*.sln'
    msbuildArguments: '/t:restore;rebuild;publish /p:DeployOnBuild=True /p:DeployDefaultTarget=WebPublish /p:WebPublishMethod=FileSystem /p:SelfContained=true /p:Platform="Any CPU" /p:Configuration=Release /p:RuntimeIdentifier=win-x64'
    msbuildVersion: latest

- task: DotNetCoreCLI@2
  inputs:
    command: 'publish'
    arguments: -c Release --runtime win-x64 /p:PublishSingleFile=true --self-contained --output $(Build.ArtifactStagingDirectory) --no-dependencies
    projects: $(Build.SourcesDirectory)\ApmBackend\ApmBackend.csproj
    zipAfterPublish: false

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'publish'
    publishLocation: 'Container'
    

您当然必须将 ApmBackend.csproj 替换为您自己的可执行项目

关于c# - Azure DevOps Pipeline 不从 Net Core 3.1 应用程序生成单个文件 exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63435781/

相关文章:

c# - 如何忽略所有标记为虚拟的属性

c# - 如何做循环并解决以下问题

c# - 为什么我的 C# .Net Core Rest API 路由找不到我的方法?

c# - ASP.Net Core 的自定义不记名 token 授权

azure - 在我毫无问题地发布我的 asp.net core web 应用程序后,azure 上的 Web 应用程序不显示它

.net-core - 无法将 Serilog 与 .Net Core 3 IHostBuilder 一起使用

c# - 正则表达式来分割数字和运算符

.net - 我可以创建 .NET Core Web API 独立应用程序吗?

.net-core - 如何测试我的模型是否已更改并需要迁移?

c# - 当我使用 VPN 连接到 SQL Server 时,如何从查询中获取 SQL Server 2008 的端口号