使用ReadyToRun或事先編譯 (AOT)
.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片段應新增到您的專案檔案.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>
完整的.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>
在更新.csproj後,重新發佈應用程式,重新部署,並重啟IIS網站或重新迴圈應用程式池。

