5

I would appreciate please some help if possible with the following problem : I try to publish my .NET 5.0 Console app via the command line, because I have to included in ansible scripts to be able to build and deploy from jenkins, and when i try the following command :

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

I get the following error:

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]

From what I've read so far this comes from the fact that dotnet is not able to publish projects with COM references. I have a .dll file as an API for accessing a remote server for getting data to my machine. I need first to regsvr32 this .dll file and then I can reference it in my project.

If I try to use the publish feature from VS2019 it works, but I don't want VS2019 on my machine on AWS, i want just to install a tool like MSBuild, which will build and release my app by running a command from an ansible playbook (so not opening VS2019 and clicking buttons to get the app published).

The solution is to use MSBuild. But how ... I don't know.

Now, after I try the following command :

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

I get the same error :

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]

The solution would be to use MSBuild directly, like :

& '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

And this works, with the problem that it does not publish the application as a single .exe app :(

Any ideas ? Thanks !

-------------------- EDIT --------------------

What works :

enter image description here

What does not work :

I added msbuild to the path and now, if I just run the following command msbuild /t:publish /p:Configuration=Release /p:TargetFramework=net5.0 I get a valid application but when I try to run it I get :

    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

This is from the fact that my .dll API likes only win32 bits and i need to compile it using the flag win-x86.

But publishing it by with 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 results in the following error :

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.

Ok, managed to solve the problem with the last error by modifying the ConsoleCoreApp1.csproject file by adding <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>

The problem of now getting 1 single .exe file containing everything is still there unfortunately.

Thanks & regards !

R13mus
  • 752
  • 11
  • 20
  • What version of Net are you using? If you are using an older version of Net from 4.7.2 you will have issue trying to publish in Core. Version of Net after 4.7.2 you can compile for either Net or Core. – jdweng Feb 09 '21 at 17:11
  • I'm using .net 5.0. In my CSPROJ File (.csproj) i use the following : net5.0. Also, above, when I'm using the command line tool, I use /p:TargetFramework=net5.0 . – R13mus Feb 09 '21 at 18:59
  • When you publish VS creates a setup.exe folder like commercial software which only updates the windows dlls and does not install VS. Part of running setup.dll is the windows dlls on the machine is updated to match the build dlls. Not sure why you don't want to publish. – jdweng Feb 09 '21 at 19:13
  • As I know COM is windows-specific technology, so you should use `net5.0-windows` instead of `net5.0` https://devblogs.microsoft.com/dotnet/announcing-net-5-0/#net-5-0-target-framework – JL0PD Feb 10 '21 at 05:39
  • If I try with net5.0-windows I get the following - 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 NETSDK1005: A ssets file 'I:\workspaceVS\net50\ConsoleCoreApp1\obj\project.assets.json' doesn't have a target for 'net5.0-windows'. Ensure that restore has run and that you have included 'net5.0-windows' in the TargetFrameworks for your project. – R13mus Feb 10 '21 at 07:04

2 Answers2

3

Actually, as you said, use msbuild -t:publish is the best way and dotnet publish cannot handle COM Reference.

And you should change to use command line, a bit change was made to your command line:

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

You have to add -p:PublishSingleFile=true.

Or you have to refer to the suggestion from this similar issue.

Mr Qian
  • 21,064
  • 1
  • 31
  • 41
  • Now, when trying to run with the additional flag -p:PublishSingleFile=true (btw is it -p: or /p:) I get : error NETSDK1047: Assets 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. – R13mus Feb 10 '21 at 07:12
  • use `msbuild -t:restore,publish xxxx`, is it successful? – Mr Qian Feb 15 '21 at 08:02
  • `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` restuls in : " (restore;publish target) (1) -> (GenerateSingleFileBundle target) -> ...\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018: The "GenerateBundle" task failed unexpectedly. [...] ...\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018: System.ArgumentException: Invalid input specification: Found multiple entries with the same BundleRelativePath. – R13mus Feb 16 '21 at 09:05
  • if I remove the flag '-p:PublishSingleFile=true' it works just that I don't get a single file, I get a folder with a lot of .dll files, etc – R13mus Feb 16 '21 at 09:08
  • Try to delete all files under `C:\Users\Administrator\.nuget\packages` and then run the command. Also, try to update your VS IDE to the latest version. The issue is quite strange. I have found [a open github issue link](https://github.com/dotnet/sdk/issues/3465). – Mr Qian Feb 19 '21 at 06:08
  • Also, check if you used this nuget package from [this link](https://github.com/dotnet/runtime/issues/3735) on your project. That share your csproj file with us could be more helpful. – Mr Qian Feb 19 '21 at 06:19
  • I added up my updated csproj file, replacing some names with '___' but i guess this is not relevant. I will try to delete all those nuget packages and try again – R13mus Feb 24 '21 at 08:18
2

The following msbuild command and options worked for my NET 6.0 project:

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

The single file application was published at:

bin\Release\net6.0\win-x64\publish
James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78