多版本Revit插件使用PostSharp与Revit_All_Main_Versions NuGet包冲突
问题背景
开发适配Revit 2023-2025的插件时,通过Revit_All_Main_Versions_API_x64 NuGet包+MSBuild条件配置实现多版本编译,原本正常。引入PostSharp的OnMethodBoundaryAspect后,编译Revit 2025版本时出现PS0264错误:
PS0264 Could not load file or assembly
'C:\Users#######.nuget\packages\revit_all_main_versions_api_x64\2025.0.0\lib\net8.0\RevitAPI.dll'.
The specified module could not be found. Build your project with
detailed verbosity to see the assembly loading log. Also check that
this project doesn't reference a project with a different processor
architecture (x86/x64/arm64).
本地直接引用Revit安装目录下的API DLL可正常编译,但该方式无法在GitHub等CI环境中使用(无本地Revit安装)。
解决方案
1. 配置PostSharp排除Revit API程序集的加载
PostSharp在织入切面时会尝试加载依赖程序集,但Revit API DLL依赖Revit主程序的模块(本地安装时存在,NuGet包中仅提供编译元数据)。可通过配置让PostSharp跳过加载Revit API程序集:
方法A:在项目文件中添加配置
<PropertyGroup> <!-- 仅对插件自身程序集应用PostSharp切面 --> <PostSharpIncludeAssemblies>$(AssemblyName).dll</PostSharpIncludeAssemblies> <!-- 排除Revit API相关程序集 --> <PostSharpExcludeAssemblies>RevitAPI.dll;RevitAPIUI.dll</PostSharpExcludeAssemblies> </PropertyGroup>
方法B:使用PostSharp.config文件
在项目根目录创建PostSharp.config,内容如下:
<?xml version="1.0" encoding="utf-8"?> <PostSharp xmlns="http://schemas.postsharp.org/1.0/configuration"> <Project> <AssemblySearch> <ExcludeAssembly Name="RevitAPI" /> <ExcludeAssembly Name="RevitAPIUI" /> </AssemblySearch> </Project> </PostSharp>
2. 确保项目平台目标与Revit API匹配
Revit仅支持x64架构,需强制项目的平台目标为x64,避免PostSharp因架构不匹配报错:
<PropertyGroup> <PlatformTarget>x64</PlatformTarget> <!-- 确保CI环境也使用x64 --> <Platform>x64</Platform> </PropertyGroup>
3. 调整NuGet包引用配置
检查Revit_All_Main_Versions_API_x64包的引用配置,确保包含必要的资产,同时确保版本匹配:
<ItemGroup> <PackageReference Include="Revit_All_Main_Versions_API_x64" Version="$(RevitVersion).*" IncludeAssets="compile; build; native" PrivateAssets="All" /> </ItemGroup>
将IncludeAssets扩展为compile; build; native,确保PostSharp能获取到完整的编译元数据。
4. CI环境兼容方案(若上述方法无效)
若必须在CI环境中使用本地引用的方式,可通过脚本在CI流程中合法获取Revit API DLL(需遵守Autodesk许可),例如在GitHub Actions中:
步骤1:添加CI环境的DLL下载脚本
jobs: build: runs-on: windows-latest steps: - name: Checkout code uses: actions/checkout@v4 - name: Download Revit 2025 API DLLs run: | # 从合法私有源下载API压缩包(示例) Invoke-WebRequest -Uri "${{ secrets.REVIT_API_2025_URL }}" -OutFile "RevitAPI_2025.zip" Expand-Archive -Path "RevitAPI_2025.zip" -DestinationPath "RevitAPIs/2025"
步骤2:项目文件中添加CI条件引用
<Choose> <!-- CI环境下使用下载的DLL --> <When Condition="$(CI) == 'true'"> <ItemGroup> <Reference Include="RevitAPI" Condition="$(Configuration.Contains('2023'))"> <HintPath>RevitAPIs/2023/RevitAPI.dll</HintPath> <Private>True</Private> </Reference> <Reference Include="RevitAPI" Condition="$(Configuration.Contains('2024'))"> <HintPath>RevitAPIs/2024/RevitAPI.dll</HintPath> <Private>True</Private> </Reference> <Reference Include="RevitAPI" Condition="$(Configuration.Contains('2025'))"> <HintPath>RevitAPIs/2025/RevitAPI.dll</HintPath> <Private>True</Private> </Reference> <!-- 同步添加RevitAPIUI.dll的引用 --> </ItemGroup> </When> <!-- 本地开发使用NuGet包 --> <Otherwise> <ItemGroup> <PackageReference Include="Revit_All_Main_Versions_API_x64" Version="$(RevitVersion).*" IncludeAssets="compile; build" PrivateAssets="All" /> </ItemGroup> </Otherwise> </Choose>
内容的提问来源于stack exchange,提问作者Stephen Chipman

