Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dotnet publish not publishing DLL to publish directory

I want to publish my self contained .NET Core (2.2) application, however one specific NuGet package (Microsoft.Management.Infrastructure) is never published to the publish folder (as in the .dll file is not present).

I'm using the command dotnet publish -c Release --self-contained -r win-x64 ConsoleApp1. When running the application inside visual studio, everything works. However when running the compiled executable, I get a FileNotFoundException:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified. at ConsoleApp1.Program.Main(String[] args)

To reproduce

1) Create a simple .NET Core console app (I tested both 2.1 and 2.2, no difference)
2) Add the NuGet package Microsoft.Management.Infrastructure
3) Add the following code to the application:

namespace ConsoleApp1
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var sessionOptions = new DComSessionOptions
            {
                Timeout = TimeSpan.FromSeconds(30)
            };

            CimSession Session = CimSession.Create("localhost", sessionOptions);

            var processors = Session.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM Win32_Processor");
            foreach (CimInstance processor in processors)
            {
                Console.WriteLine(processor.CimInstanceProperties["Name"].Value);
            }

            Console.ReadLine();
        }
    }
}

4) Publish the project: dotnet publish -c Release --self-contained -r win-x64 ConsoleApp1
5) Run the compiled executable

like image 416
Devator Avatar asked Feb 22 '19 15:02

Devator


People also ask

How do I publish a project in dotnet?

To make publish output go to separate folders for each project, specify a relative path by using the msbuild PublishDir property instead of the --output option. For example, dotnet publish -p:PublishDir=. \publish sends publish output for each project to a publish folder under the folder that contains the project file.

How do I publish a SLN file?

Open your web project in Visual Studio (using the WebSite. sln or WebApp. sln file). Right-click the Kentico project in the Solution Explorer and select Publish Web Site (web site projects) or Publish (web applications).

What is the difference between dotnet build and dotnet publish?

Build compiles the source code into a (hopefully) runnable application. Publish takes the results of the build, along with any needed third-party libraries and puts it somewhere for other people to run it.


1 Answers

The MMI package is composed of multiple assemblies which are specific to the Windows version i.e.: win10-x64, win10-x86 win8-x64 etc.

Thereby, you must use a version-specific target runtime (for example: win10-x64) instead of the generic win-x64. Using the publish command from below, the MMI DLL is included in the publishing procedure.

dotnet publish -c Release --self-contained -r win10-x64 ConsoleApp1
like image 140
prd Avatar answered Oct 06 '22 17:10

prd