.NET Framework 4.8 Windows Service 啟動例外

This article was translated from English: Does it need improvement?
Translated
View the article in English

一個 IronPDF .NET Framework 4.8 Windows Service 專案在建構 FileNotFoundException 時拋出 ChromePdfRenderer,或在設定授權金鑰時拋出 StackOverflowException。 相同的程式碼在控制台或 WinForms 主機中正常工作。

MSBuild 的facade檢測邏輯防止六個NuGet提供的程式集複製到輸出目錄:System.Text.Encodings.WebSystem.Text.Encoding.CodePages。 MSBuild 認為這些程式集的內建 .NET 4.8 版本等同於 NuGet 的版本,但 App.config 中的綁定重定向需要使用 NuGet 的版本。 缺少的檔案導致啟動時出現例外。

解決方案

選項 1:遷移到 PackageReference(推薦)

在 Visual Studio 中右鍵點擊 packages.config,然後選擇將 packages.config 遷移到 PackageReference。 確認 <AutoGenerateBindingRedirects>.csproj 文件中已啟用,然後重新構建。 PackageReference 的 MSBuild 整合不會觸發 facade 跳過行為。

選項 2:使用 AfterBuild 目標強制複製 facade DLL

如果需要保留 packages.config,請將此 MSBuild 目標新增到 .csproj 文件中:

<Target Name="CopyFacadeAssemblies" AfterTargets="Build">
    <ItemGroup>
        <FacadeAssemblies Include="..\packages\System.Buffers.4.5.1\lib\netstandard2.0\System.Buffers.dll" />
        <FacadeAssemblies Include="..\packages\System.Text.Encodings.Web.6.0.1\lib\net461\System.Text.Encodings.Web.dll" />
        <FacadeAssemblies Include="..\packages\System.Text.Json.6.0.11\lib\net461\System.Text.Json.dll" />
        <FacadeAssemblies Include="..\packages\System.Text.Encoding.CodePages.5.0.0\lib\net461\System.Text.Encoding.CodePages.dll" />
        <FacadeAssemblies Include="..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll" />
        <FacadeAssemblies Include="..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll" />
    </ItemGroup>
    <Copy SourceFiles="@(FacadeAssemblies)" DestinationFolder="$(OutputPath)" SkipUnchangedFiles="true" />
</Target>
<Target Name="CopyFacadeAssemblies" AfterTargets="Build">
    <ItemGroup>
        <FacadeAssemblies Include="..\packages\System.Buffers.4.5.1\lib\netstandard2.0\System.Buffers.dll" />
        <FacadeAssemblies Include="..\packages\System.Text.Encodings.Web.6.0.1\lib\net461\System.Text.Encodings.Web.dll" />
        <FacadeAssemblies Include="..\packages\System.Text.Json.6.0.11\lib\net461\System.Text.Json.dll" />
        <FacadeAssemblies Include="..\packages\System.Text.Encoding.CodePages.5.0.0\lib\net461\System.Text.Encoding.CodePages.dll" />
        <FacadeAssemblies Include="..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll" />
        <FacadeAssemblies Include="..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll" />
    </ItemGroup>
    <Copy SourceFiles="@(FacadeAssemblies)" DestinationFolder="$(OutputPath)" SkipUnchangedFiles="true" />
</Target>
XML

調整版本號碼以匹配在您的 packages 目錄中還原的確切版本。

在 facade 參考上設定 <Private>True</Private>(本地複製)無法解決問題; MSBuild 的facade檢測仍然阻止複製步驟。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備開始了嗎?
Nuget 下載 20,088,359 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想快速獲得證明嗎? PM > Install-Package IronPdf
執行範例 看您的HTML變成PDF。