首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用MSBuild.exe发布net5.0控制台应用程序,因为dotnet不能工作

使用MSBuild.exe发布net5.0控制台应用程序,因为dotnet不能工作
EN

Stack Overflow用户
提问于 2021-02-09 15:50:19
回答 2查看 6K关注 0票数 4

如果可能的话,我希望能提供一些帮助,解决以下问题:我尝试通过命令行发布我的.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的发布功能,它可以工作,但我不希望VS2019出现在AWS上,我只想安装一个像MSBuild这样的工具,它将通过运行ansible剧本中的命令来构建和发布我的应用程序(所以不要打开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应用程序发布:(

有什么想法吗?谢谢!

什么起作用:

不起作用的是:

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

代码语言:javascript
运行
复制
    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发布它会导致以下错误:

代码语言:javascript
运行
复制
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.

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

代码语言:javascript
运行
复制
<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>

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

谢谢和问候!

EN

回答 2

Stack Overflow用户

发布于 2021-02-10 04:58:50

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

您应该更改为使用命令行,对命令行做了一些更改:

代码语言:javascript
运行
复制
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的建议。

票数 2
EN

Stack Overflow用户

发布于 2022-02-24 14:45:51

以下msbuild命令和选项适用于我的Net6.0项目:

代码语言:javascript
运行
复制
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

单个文件应用程序发布于:

代码语言:javascript
运行
复制
bin\Release\net6.0\win-x64\publish
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66122391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档