我在 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,Version=1.0.0.0,Culture=neutral,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)
- 微軟 .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 代碼中,它會因上述錯誤而失敗。有任何想法嗎?
EDIT 1: The error does also occur when i run the .exe directly form the command line instead of starting the IIS instance.
EDIT 2: Every DLL that is in the bin\debug folder (the one i use for testing on my laptop and that runs fine) is also contained in the bin\release folder (the one that gets published to the IIS). There are DLL that are in the release folder but not in the debug folder:
- 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 版本。
uj5u.com熱心網友回復:
我通過在目標服務器上構建/發布應用程式解決了這個問題。專案或代碼中沒有任何變化。IIS 也保持不變。在服務器上本地構建后,它現在就可以作業了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/446534.html
