ArcGIS Pro 3.1插件开发中System.ServiceModel.Primitives版本冲突求助
ArcGIS Pro 3.1与System.ServiceModel.Primitives版本冲突解决
问题背景
基于.NET 6开发ArcGIS Pro 3.1工具时,项目依赖的某NuGet包需要System.ServiceModel.Primitives 6.0版本,但ArcGIS Pro 3.1安装目录的bin文件夹自带该程序集的4.9版本。加载模块时触发TypeLoadException:
无法从程序集'System.ServiceModel.Primitives, Version=4.9.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'中加载类型'System.ServiceModel.DuplexChannelFactory`1'
替换ArcGIS bin目录内的4.9版本为6.0版本可临时解决,但属于不合规操作,恢复后错误重现。已尝试绑定重定向、手动加载DLL、直接添加程序集引用等方法均无效,且无法移除依赖该NuGet包的代码。
项目包含Geospatial、Core、System、ArcGIS、Desktop五个工程,Desktop依赖其余四个工程,工程配置代码如下:
[此处粘贴原工程配置代码]
合规解决方案
1. 配置ArcGIS Pro用户级程序集绑定
ArcGIS Pro允许通过用户配置目录自定义程序集绑定,无需修改安装目录文件:
- 定位到ArcGIS Pro用户配置目录:
C:\Users\<你的用户名>\Documents\ArcGIS\Profiles\<你的配置文件名> - 创建或编辑
ArcGIS.exe.config,添加以下绑定规则:
<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.ServiceModel.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="4.9.0.0" newVersion="6.0.0.0" /> <codeBase version="6.0.0.0" href="file:///[你的项目输出绝对路径]/System.ServiceModel.Primitives.dll" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration>
替换[你的项目输出绝对路径]为6.0版本DLL所在的实际路径。
2. 强化项目绑定重定向配置
在Desktop工程中确保绑定重定向规则被正确生成并嵌入AddIn包:
- 编辑Desktop工程的
.csproj文件,添加以下属性:
<PropertyGroup> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType> </PropertyGroup>
- 构建项目后,检查输出目录下的
Desktop.dll.config,确认包含正确的绑定重定向规则,将该配置文件与AddIn包一同部署。
3. 自定义程序集加载上下文隔离版本
通过AssemblyLoadContext隔离加载6.0版本的程序集,避免与ArcGIS Pro自带版本冲突:
using System.Runtime.Loader; using System.IO; using System.Reflection; public class SvcModelLoadContext : AssemblyLoadContext { private readonly string _dllDir; public SvcModelLoadContext(string dllDirectory) : base(isCollectible: false) { _dllDir = dllDirectory; } protected override Assembly Load(AssemblyName assemblyName) { if (assemblyName.Name.Equals("System.ServiceModel.Primitives", StringComparison.OrdinalIgnoreCase)) { var dllPath = Path.Combine(_dllDir, "System.ServiceModel.Primitives.dll"); return LoadFromAssemblyPath(dllPath); } // 其他程序集交由默认上下文加载 return null; } } // 在模块初始化阶段执行(例如在IPlugInImplementation的Initialize方法中) var loadContext = new SvcModelLoadContext(@"[你的项目输出目录]"); loadContext.LoadFromAssemblyName(new AssemblyName("System.ServiceModel.Primitives, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"));
确保在调用依赖该程序集的代码前完成加载上下文的初始化。
内容的提问来源于stack exchange,提问作者ZZZ
相关产品推荐
相关产品推荐

