【发布时间】:2019-02-22 15:06:40
【问题描述】:
我想发布我的自包含 .NET Core (2.2) 应用程序,但是一个特定的 NuGet 包 (Microsoft.Management.Infrastructure) 从未发布到 publish 文件夹(因为 .dll 文件不存在)。
我正在使用命令dotnet publish -c Release --self-contained -r win-x64 ConsoleApp1。在 Visual Studio 中运行应用程序时,一切正常。但是,在运行编译后的可执行文件时,我得到一个 FileNotFoundException:
未处理的异常:System.IO.FileNotFoundException:无法加载 文件或程序集“Microsoft.Management.Infrastructure, 版本=1.0.0.0,文化=中性,PublicKeyToken=31bf3856ad364e35'。 该系统找不到指定的文件。在 ConsoleApp1.Program.Main(String[] args)
重现
1) 创建一个简单的 .NET Core 控制台应用程序(我测试了 2.1 和 2.2,没有区别)
2) 添加NuGet包Microsoft.Management.Infrastructure
3) 将以下代码添加到应用程序中:
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) 发布项目:dotnet publish -c Release --self-contained -r win-x64 ConsoleApp1
5) 运行编译后的可执行文件
【问题讨论】: