問題描述

最近以 ASP.NET Core Web Application 執行 Powershell Core 指令來完成某些工作,在開發期間完全沒有什麼問題,但佈署在 IIS 上時卻發生找不到套件的問題,

錯誤內容如下:


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.
File name: 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'


看到這個錯誤訊息著實模不著頭緒,一直往沒有引用套件方向找,最後再 Powershell github issue 才發現在發布設定上,若使用 win-x86 或 win-x64 設定,會遺失部分 dll。若要解決這個問題,目前需要設定為 win10-x86、win10-x64、win7-x86、win7-x64 其中之一。

而在 ASP.NET Core 3.0 版本編輯發佈檔案時,可以選擇的設定只有 win-x86 與 win-x64。


Solution:
我們可以開啟發佈檔案 Properties > PublishProfiles 內選擇自己的發佈檔案進行編輯 (如下圖,使用預設名稱 FolderProfile.pubxml),手動將 RuntimeIdentifier 改成 win10-x64 ,在重新發佈與佈署即可。 這應該是暫時性的錯誤,期望在未來會修掉。



參考資料: