You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

单应用同时使用CRM2011与365早期绑定类报account代理类型重复错误

解决方案

方法1:为每个服务实例指定专属的代理类型行为

冲突根源是两个CRM版本的早期绑定类(如Account)来自不同程序集,但服务代理默认会扫描所有加载的程序集导致类型混淆。需为每个服务实例明确指定对应的早期绑定程序集:

配置CRM2011代理

在获取OrganizationServiceProxy后,移除默认代理行为,添加CRM2011类库专属的行为:

using (OrganizationServiceProxy crmProxy = CrmHelper.GetServiceProxy(config.CrmUri, _syncU[1], config.SerwisUserPassword, _syncU[0]))
{
    // 移除全局默认的代理类型行为
    crmProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Remove<ProxyTypesBehavior>();
    // 添加CRM2011早期绑定类所在的程序集
    crmProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior(typeof(CRM2011.Account).Assembly));
    
    CRM2011.Account accCRM = crmProxy.Retrieve(CRM2011.Account.EntityLogicalName, Guid.Parse("C67CF43C-D6AE-ED11-A4A2-00155D00E457"), new ColumnSet(true)).ToEntity<CRM2011.Account>();
    name = accCRM.Name;
}

配置CRM365客户端

创建CrmServiceClient后,获取其内部的OrganizationServiceProxy,同样指定CRM365的早期绑定程序集:

string connectionString = "AuthType=Office365;Username = username;Url = https://****.api.crm4.dynamics.com;Password = password;";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
CrmServiceClient conn = new CrmServiceClient(connectionString);

if (conn.OrganizationServiceProxy != null)
{
    // 移除默认行为,添加CRM365专属程序集
    conn.OrganizationServiceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Remove<ProxyTypesBehavior>();
    conn.OrganizationServiceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior(typeof(CRM365.Account).Assembly));
}

// 正常使用早期绑定类创建数据
CRM365.Account acc = new CRM365.Account();
acc.Name = name;
conn.Create(acc);

方法2:拆分操作到独立程序集

将CRM2011的操作封装到单独的类库项目,CRM365的操作封装到另一个类库项目,控制台应用仅调用这两个类库的公开方法。这种方式通过隔离AppDomain内的类型加载,从根源避免类型冲突。

方法3:强制命名空间限定

在代码中全程使用完整命名空间(如CRM2011.Account、CRM365.Account),不使用using CRM2011;或using CRM365;导入命名空间。此方法需配合方法1使用,仅靠命名空间无法解决服务代理的程序集扫描冲突。


内容的提问来源于stack exchange,提问作者slawur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 19:40:40