使用ReadyToRun或事先編譯 (AOT)

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

.NET ReadyToRun (R2R) 是事先編譯 (AOT) 的一種形式。

在部署時啟用ReadyToRun編譯可能會違反防篡改保護,並導致以下型別的例外情況:

Exception: Unhandled exception. IronSoftware.Exceptions.LicensingException: IronPdf, Version=2024.2.0.2, Culture=neutral, PublicKeyToken=94e1c31412563c75 assembly is not authentic. Please try to reinstall the nuget package
   at IronPdf.PdfDocument.uswvws(Boolean vhfwdf)
   at IronPdf.PdfDocument.get_BinaryData()
   at IronPdf.PdfDocument.SaveAs(String FileName, Boolean SaveAsRevision)
   at Program.<Main>(String[] args) in C:\csharppro\aottest\aottest\Program.cs:line 5

原因

根據Microsoft的文件,SDK將預編譯與應用程式一起分發的組件。 對於自包含應用程式,此組件集將包括框架。 重要的是要注意,C++/CLI二進制檔案不符合ReadyToRun編譯的資格。

解決方案

要將特定組件排除在ReadyToRun處理之外,請在您的專案檔案中使用IronPdf.dll組件:

<ItemGroup>

    <PublishReadyToRunExclude Include="IronPdf.dll" />
</ItemGroup>
<ItemGroup>

    <PublishReadyToRunExclude Include="IronPdf.dll" />
</ItemGroup>
XML

此XML片段應新增到您的專案檔案.csproj以防止指定的組件與ReadyToRun一起預編譯,從而避免潛在的授權或篡改問題。

在首次IronPDF呼叫時發生System.Environment.FailFast

在某些Windows部署中,IronPDF在第一次IronPDF呼叫時立即使進程崩潰,且沒有應用程式層級的日誌輸出。 Windows事件日誌記錄了一個類似的條目:

The application requested process termination through System.Environment.FailFast.
Message: Stack: at System.Environment.FailFast(System.String)
  at <Module>..cctor()
  at Program.<Main>$(System.String[])

這與上述的授權例外不同。 崩潰發生在靜態初始化期間,在正常日誌可用之前,因此在進程終止之前沒有IronPDF的日誌行出現。

此情況已在Windows Server上IIS中託管的ASP.NET Core應用程式中確認啟用了PublishReadyToRun。 已在以下環境中重現:

  • IronPDF 2026.4.1
  • .NET 10
  • Windows Server 2025 Datacenter上的IIS (w3wp.exe)
  • AWS EC2 win-x64 發佈目標

解決方案

保留應用程式的IronPdf.dll

<ItemGroup>
    <PublishReadyToRunExclude Include="IronPdf.dll" />
</ItemGroup>
<ItemGroup>
    <PublishReadyToRunExclude Include="IronPdf.dll" />
</ItemGroup>
XML

完整的.csproj範例:

<PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>

<ItemGroup>
    <PackageReference Include="IronPdf" Version="2026.4.1" />
</ItemGroup>

<ItemGroup>
    <PublishReadyToRunExclude Include="IronPdf.dll" />
</ItemGroup>
<PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>

<ItemGroup>
    <PackageReference Include="IronPdf" Version="2026.4.1" />
</ItemGroup>

<ItemGroup>
    <PublishReadyToRunExclude Include="IronPdf.dll" />
</ItemGroup>
XML

在更新.csproj後,重新發佈應用程式,重新部署,並重啟IIS網站或重新迴圈應用程式池。

Curtis Chau
技術作家

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

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

準備開始了嗎?
Nuget 下載 19,936,792 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在捲動嗎?

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