【问题标题】:Using MSBuild.exe to release a net5.0 console application because dotnet not working使用 MSBuild.exe 发布 net5.0 控制台应用程序,因为 dotnet 不工作
【发布时间】:2021-02-09 15:50:19
【问题描述】:

如果可能,我将不胜感激,请为以下问题提供一些帮助:我尝试通过命令行发布我的 .NET 5.0 控制台应用程序,因为我必须包含在 ansible 脚本中才能从 jenkins 构建和部署,并且当我尝试以下命令时:

dotnet publish --configuration Release -p:PublishReadyToRun=true -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true --runtime win-x86 --framework net5.0

我收到以下错误:

C:\Program Files\dotnet\sdk\5.0.102\Microsoft.Common.CurrentVersion.targets(2744,5): error : MSB4803: The task "ResolveComReference" is not supported on the .NET Core version of MSBuild. Please use the .NET Framework version of MSBuild. See https://aka.ms/msbuild/MSB4803 for further details. [I:\workspaceVS\net50\ConsoleCoreApp1\ConsoleCoreApp1.csproj]

从我目前所读到的内容来看,这是因为 dotnet 无法发布带有 COM 引用的项目。我有一个 .dll 文件作为 API,用于访问远程服务器以将数据传输到我的机器。我需要先 regsvr32 这个 .dll 文件,然后我可以在我的项目中引用它。

如果我尝试使用 VS2019 的发布功能,它可以工作,但我不想在 AWS 上的机器上安装 VS2019,我只想安装一个类似 MSBuild 的工具,它将通过运行命令来构建和发布我的应用程序来自 ansible playbook(所以不要打开 VS2019 并单击按钮来发布应用程序)。

解决方案是使用MSBuild。但是如何......我不知道。

现在,在我尝试以下命令后:

dotnet msbuild ConsoleCoreApp1.csproj /t:publish /p:Configuration=Release /p:TargetFramework=net5.0 /p:SelfContained=true /p:PublishTrimmed=True /p:PublishReadyToRun=True /p:RuntimeIdentifier=win-x86 /p:PublishDir=bin\Release

我得到同样的错误:

C:\Program Files\dotnet\sdk\5.0.102\Microsoft.Common.CurrentVersion.targets(2744,5): error : MSB4803: The task "ResolveComReference" is not supported on the .NET Core version of MSBuild. Please use the .NET Framework version of MSBuild. See https://aka.ms/msbuild/MSB4803 for further details. [I:\workspaceVS\net50\ConsoleCoreApp1\ConsoleCoreApp1.csproj]

解决方案是直接使用 MSBuild,例如:

& 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe' /t:publish /p:Configuration=Release /p:TargetFramework=net5.0 /p:SelfContained=true /p:PublishTrimmed=True /p:PublishReadyToRun=True /p:RuntimeIdentifier=win-x86 /p:PublishDir=bin\Release

这可行,但问题是它不会将应用程序作为单个 .exe 应用程序发布:(

有什么想法吗?谢谢!

-------------------- 编辑 --------

什么有效:

什么不起作用:

我将 msbuild 添加到路径中,现在,如果我只运行以下命令 msbuild /t:publish /p:Configuration=Release /p:TargetFramework=net5.0 我得到了一个有效的应用程序,但是当我尝试运行它时,我得到了:

    Unhandled exception. System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {CE92C3B9-9A93-40E1-85AB-6A49170AEF7F} failed due to the following error: 80040154 Class not registered (0x80040154 (REGDB_E_CLASSNOTREG)).
   at ConsoleApp1.Service1..ctor(String[] args) in I:\workspaceVS\net50\ConsoleCoreApp1\Service1.cs:line 24
   at ConsoleCoreApp1.Program.Main(String[] args) in I:\workspaceVS\net50\ConsoleCoreApp1\Program.cs:line 7

这是因为我的 .dll API 只喜欢 win32 位,我需要使用标志 win-x86 编译它。

但是使用 msbuild /t:publish /p:Configuration=Release /p:TargetFramework=net5.0 /p:SelfContained=true /p:PublishTrimmed=True /p:PublishReadyToRun=True /p:RuntimeIdentifier=win-x86 -p:PublishSingleFile=true 发布它会导致以下错误:

Build FAILED.

"I:\workspaceVS\net50\ConsoleCoreApp1\ConsoleCoreApp1.csproj" (publish target) (1) ->
(ResolvePackageAssets target) ->
  C:\Program Files\dotnet\sdk\5.0.102\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(241,5): error NETSDK1047: A
ssets file 'I:\workspaceVS\net50\ConsoleCoreApp1\obj\project.assets.json' doesn't have a target for 'net5.0/win-x86'. Ensure that restore has
run and that you have included 'net5.0' in the TargetFrameworks for your project. You may also need to include 'win-x86' in your project's Run
timeIdentifiers.

好的,通过修改ConsoleCoreApp1.csproject文件添加<RuntimeIdentifier>win-x86</RuntimeIdentifier>,设法解决了最后一个错误的问题:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
    <RuntimeIdentifier>win-x86</RuntimeIdentifier>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <Prefer32Bit>true</Prefer32Bit>
    <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
    <Prefer32Bit>true</Prefer32Bit>
    <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>

  <ItemGroup>
    <COMReference Include="GV8APILib.dll">
      <WrapperTool>tlbimp</WrapperTool>
      <VersionMinor>1</VersionMinor>
      <VersionMajor>1</VersionMajor>
      <Guid>0a67e301-3ecb-47be-bba9-dc67ff219358</Guid>
      <Lcid>0</Lcid>
      <Isolated>false</Isolated>
    </COMReference>
  </ItemGroup>

  <ItemGroup>
    <Folder Include="FileReading\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.2" />
    <PackageReference Include="NLog" Version="4.7.7" />
    <PackageReference Include="NLog.Config" Version="4.7.7" />
    <PackageReference Include="NLog.Schema" Version="4.7.7" />
    <PackageReference Include="RabbitMQ.Client" Version="6.2.1" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
    <PackageReference Include="System.Text.Json" Version="5.0.1" />
  </ItemGroup>


  <ItemGroup>    
    <None Update="NLog.config">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="NLog.xsd">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="Others\___.key">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="Others\___.p12">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="Others\rmq___.uat.key">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="Others\rmq___.uat.p12">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="Others\rmq___.uat.pem">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="XMLRequest\AllOrders.xml">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="XMLRequest\Companies.xml">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="XMLRequest\DealsOrders - RequestAllDefinitions.xml">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="XMLRequest\DealsOrders.xml">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="XMLRequest\RequestHistTrades.xml">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="XMLRequest\InstrumentDefinitionsQuery.xml">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="XMLRequest\Orders.xml">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="XMLRequest\QueryOutput.xml">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="XMLRequest\SequenceItemsQuery.xml">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="Libraries\___.GvApi.dll">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="Libraries\System.Threading.Tasks.Dataflow.dll">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="Libraries\___.GvApi.dll">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="Libraries\___.GvApi.Managed.dll">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>

</Project>

不幸的是,现在获取 1 个包含所有内容的单个 .exe 文件的问题仍然存在。

感谢和问候!

【问题讨论】:

  • 您使用的是什么版本的网络?如果您使用的是 4.7.2 中的较旧版本的 Net,您将在尝试在 Core 中发布时遇到问题。 4.7.2 之后的 Net 版本,您可以为 Net 或 Core 编译。
  • 我使用的是 .net 5.0。在我的 CSPROJ 文件 (.csproj) 中,我使用以下内容:net5.0。另外,在上面,当我使用命令行工具时,我使用 /p:TargetFramework=net5.0 。
  • 当你发布 VS 时会创建一个 setup.exe 文件夹,就像商业软件一样,它只更新 windows dll,不安装 VS。运行 setup.dll 的一部分是更新机器上的 windows dll 以匹配构建 dll。不知道你为什么不想发布。
  • 据我所知 COM 是特定于 Windows 的技术,因此您应该使用 net5.0-windows 而不是 net5.0 devblogs.microsoft.com/dotnet/announcing-net-5-0/…
  • 如果我尝试使用 net5.0-windows,我会得到以下结果 - 构建失败。 “I:\workspaceVS\net50\ConsoleCoreApp1\ConsoleCoreApp1.csproj”(发布目标)(1)->(ResolvePackageAssets 目标)-> C:\Program Files\dotnet\sdk\5.0.102\Sdks\Microsoft.NET.Sdk \targets\Microsoft.PackageDependencyResolution.targets(241,5): error NETSDK1005: A ssets 文件 'I:\workspaceVS\net50\ConsoleCoreApp1\obj\project.assets.json' 没有目标 'net5.0-视窗'。确保恢复已运行,并且您已在项目的 TargetFrameworks 中包含“net5.0-windows”。

标签: c# .net msbuild visual-studio-2019


【解决方案1】:

实际上,正如你所说,使用msbuild -t:publish 是最好的方法,dotnet publish 无法处理 COM 引用。

你应该改用命令行,你的命令行做了一点改动:

msbuild /t:publish /p:Configuration=Release /p:TargetFramework=net5.0 /p:SelfContained=true /p:PublishTrimmed=True /p:PublishReadyToRun=True /p:RuntimeIdentifier=win-x86  -p:PublishSingleFile=true

您必须添加-p:PublishSingleFile=true

或者你得参考this similar issue的建议。

【讨论】:

  • 现在,当尝试使用附加标志 -p:PublishSingleFile=true(顺便说一句是 -p: 或 /p:) 运行时,我得到:错误 NETSDK1047:资产文件 'I:\workspaceVS\ net50\ConsoleCoreApp1\obj\project.assets.json' 没有 'net5.0/win-x86' 的目标。确保恢复已运行,并且您已在项目的 TargetFrameworks 中包含“net5.0”。
  • 使用msbuild -t:restore,publish xxxx,成功了吗?
  • msbuild /t:restore /t:publish /p:Configuration=Release /p:TargetFramework=net5.0 /p:SelfContained=true /p:PublishTrimmed=True /p:PublishReadyToRun=True /p:RuntimeIdentifier=win-x86 -p:PublishSingleFile=true resuls in : " (restore;publish target) (1) -> (GenerateSingleFileBundle target) -> ...\targets\Microsoft.NET.Publish.targets(1016,5) :错误 MSB4018:“GenerateBundle”任务意外失败。[...] ...\targets\Microsoft.NET.Publish.targets(1016,5):错误 MSB4018:System.ArgumentException:无效输入规范:找到多个条目具有相同的 BundleRelativePath。
  • 如果我删除标志 '-p:PublishSingleFile=true' 它只是因为我没有得到一个文件,我得到一个包含很多 .dll 文件的文件夹,等等
  • 尝试删除C:\Users\Administrator\.nuget\packages下的所有文件,然后运行命令。另外,尝试将您的 VS IDE 更新到最新版本。这个问题很奇怪。我找到了a open github issue link
【解决方案2】:

以下 msbuild 命令和选项适用于我的 NET 6.0 项目:

msbuild -property:Configuration=Release;IncludeAllContentForSelfExtract=true;Platform="Any CPU";PublishReadyToRun=true;PublishSingleFile=true;PublishTrimmed=True;Runtimeidentifier=win-x64;SelfContained=true -restore -target:publish SingleFileApp.sln

单文件应用发布于:

bin\Release\net6.0\win-x64\publish

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 2018-02-09
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    相关资源
    最近更新 更多