Devator
Devator

Reputation: 3904

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

Upvotes: 12

Views: 6329

Answers (1)

prd
prd

Reputation: 2311

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

Upvotes: 17

Related Questions