【发布时间】:2022-10-04 23:15:17
【问题描述】:
我在 IIS 上运行 ASP.NET Core 6 应用程序作为 Rest Api,为特定任务调用 Powershell 脚本。它在我的笔记本电脑(Windows 10)上运行良好,但当我在 Windows Server 2019 版本 1809 Build 17763.1935 上运行它时不起作用。该错误告诉我它找不到程序集“Microsoft.Management.Infrastructure”。
未处理的异常。 System.IO.FileNotFoundException:无法加载 文件或程序集“Microsoft.Management.Infrastructure, 版本=1.0.0.0,文化=中性,PublicKeyToken=31bf3856ad364e35'。 Das System kann die angegebene Datei nicht finden。文件名: 'Microsoft.Management.Infrastructure,版本=1.0.0.0, 文化=中性,PublicKeyToken=31bf3856ad364e35'
“Das System kann die angegebene Datei nicht finden。” = "找不到文件。"
有没有人也遇到过这个问题?服务器安装了以下东西:
- Microsoft .NET 6.0.3 - Windows 服务器托管 Microsoft .NET 运行时
- 6.0.3 (x64) Microsoft .NET 运行时 - 6.0.3 (x86)
- Microsoft .NET SDK 6.0.201 (x64) 微软
- ASP.NET Core 6.0.3 - 共享框架 (x64)
- Microsoft ASP.NET Core 6.0.3 - 共享框架 (x86)
- Microsoft Visual C++ 2015-2019 可再发行版 (x64) - 14.28.29913
- Microsoft Visual C++ 2015-2019 可再发行版 (x86) - 14.28.29913
- IIS 10.0
- Windows PowerShell 5.1
- PowerShell 7.2.1
现在来测试它是否是服务器设置丢失了我用这段代码编写的一个小的 .net 控制台应用程序
using System.Management.Automation; using System.Management.Automation.Runspaces; using Microsoft.PowerShell; var initialSessionState = InitialSessionState.CreateDefault(); initialSessionState.ExecutionPolicy = ExecutionPolicy.Unrestricted; using (PowerShell powerShell = PowerShell.Create(initialSessionState)) { powerShell.AddCommand("whoami"); foreach (var item in powerShell.Invoke()) { Console.WriteLine(item.BaseObject.ToString()); } if (powerShell.HadErrors) { throw new Exception("powershell script had errors"); } }我可以毫无问题地在服务器上运行这个程序。但是,如果我将这个确切的代码复制粘贴到我的 Api 代码中,它会因上述错误而失败。有任何想法吗?
编辑 1:当我直接从命令行运行 .exe 而不是启动 IIS 实例时,也会发生该错误。
编辑 2: bin\debug 文件夹中的每个 DLL(我在笔记本电脑上用于测试并且运行良好的那个)也包含在 bin\release 文件夹(发布到 IIS 的那个)中。发布文件夹中有 DLL,但调试文件夹中没有:
- Microsoft.Management.Infrastructure.CimCmdlets.dll
- Microsoft.PowerShell.Commands.Diagnostics.dll
- Microsoft.PowerShell.Commands.Management.dll
- Microsoft.PowerShell.Commands.Utility.dll
- Microsoft.PowerShell.ConsoleHost.dll
- Microsoft.PowerShell.CoreCLR.Eventing.dll
- Microsoft.PowerShell.SDK.dll Microsoft.PowerShell.Security.dll
- Microsoft.WSMan.Management.dll Microsoft.WSMan.Runtime.dll
- PowerShell.Core.Instrumentation.dll pwrshplugin.dll sni.dll
- System.Management.Automation.dll
文件“Microsoft.Management.Infrastructure.dll”既不在发布文件夹中,也不在调试文件夹中。
项目 csproj 文件如下所示:
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net6.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <!-- https://github.com/nhibernate/nhibernate-core/issues/2603 --> <EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.2" /> <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.2.1" /> <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.2.1" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> <PackageReference Include="System.DirectoryServices" Version="6.0.0" /> <PackageReference Include="System.DirectoryServices.AccountManagement" Version="6.0.0" /> </ItemGroup> </Project>编辑 3: 通过扩展 csproj 文件
<PackageReference Include="Microsoft.Management.Infrastructure" Version="2.0.0" /> <PackageReference Include="Microsoft.Management.Infrastructure.CimCmdlets" Version="7.2.2" /> <PackageReference Include="Microsoft.Management.Infrastructure.Runtime.Win" Version="2.0.0" />也不起作用。还引用“Microsoft.Management.Infrastructure”版本 1.0.0 而不是 2.0.0 不起作用,因为“System.Management.Automation”似乎需要该软件包的 2.0.0 版本。
【问题讨论】:
-
查看代码正在运行的 bin 文件夹,并查看文件夹中有哪些 dll。
标签: c# asp.net .net powershell asp.net-core