Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用MSBuild.exe释放net5.0控制台应用程序,因为dotnet不工作_C#_.net_Msbuild_Visual Studio 2019 - Fatal编程技术网

C# 使用MSBuild.exe释放net5.0控制台应用程序,因为dotnet不工作

C# 使用MSBuild.exe释放net5.0控制台应用程序,因为dotnet不工作,c#,.net,msbuild,visual-studio-2019,C#,.net,Msbuild,Visual Studio 2019,如果可能的话,我希望您能帮助我解决以下问题:我尝试通过命令行发布我的.NET 5.0 Console应用程序,因为我必须包含在ansible脚本中才能从jenkins构建和部署,并且当我尝试以下命令时: dotnet发布--配置发布-p:PublishReadyToRun=true-p:PublishSingleFile=true-p:PublishTrimmed=true--自包含的true--运行时win-x86--framework net5.0 我得到以下错误: C:\Program F

如果可能的话,我希望您能帮助我解决以下问题:我尝试通过命令行发布我的.NET 5.0 Console应用程序,因为我必须包含在ansible脚本中才能从jenkins构建和部署,并且当我尝试以下命令时:

dotnet发布--配置发布-p:PublishReadyToRun=true-p:PublishSingleFile=true-p:PublishTrimmed=true--自包含的true--运行时win-x86--framework net5.0

我得到以下错误:

C:\Program Files\dotnet\sdk\5.0.102\Microsoft.Common.CurrentVersion.targets(2744,5):错误:MSB4803:MSBuild的.NET核心版本不支持任务“ResolveComReference”。请使用.NET Framework版本的MSBuild。看见https://aka.ms/msbuild/MSB4803 详情请参阅。[I:\workspaceVS\net50\ConsoleRecreap1\ConsoleRecreap1.csproj]

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

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

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

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

dotnet msbuild consolecorepea1.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):错误:MSB4803:MSBuild的.NET核心版本不支持任务“ResolveComReference”。请使用.NET Framework版本的MSBuild。看见https://aka.ms/msbuild/MSB4803 详情请参阅。[I:\workspaceVS\net50\ConsoleRecreap1\ConsoleRecreap1.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.
好的,通过添加
win-x86
,修改了consolecorepea1.csproject文件,解决了上一个错误的问题:


Exe
net5.0
win-x86
真的
x86
真的
x86
tlbimp
1.
1.
0a67e301-3ecb-47be-bba9-dc67ff219358
0
假的
总是
总是
总是
总是
总是
总是
总是
保存最新
保存最新
保存最新
保存最新
保存最新
保存最新
保存最新
保存最新
保存最新
总是
总是
总是
总是
不幸的是,现在获取一个包含所有内容的.exe文件的问题仍然存在


谢谢和问候!

事实上,正如您所说,使用
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


您必须参考来自的建议。

您使用的是什么版本的Net?如果您使用的是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
如果我尝试使用net5.0-windows,我会得到以下结果-生成失败。“I:\workspaceVS\net50\consolecorepea1\consolecorepea1.csproj”(发布目标)(1)->(ResolvePackageAssets目标)->C:\程序文件\
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