【发布时间】:2015-11-23 14:07:28
【问题描述】:
您好,我有一个调用 WCF 服务的 CRM 2013 插件,该服务失败并出现以下错误:
'通讯对象, System.ServiceModel.ChannelFactory`1[ISupplyClaimsService],不能 在Opening状态下修改',
我有时也会发现,当调用服务时,插件注册工具会崩溃。是否可以从插件调用 WCF 服务?我在网上看到了一些帖子,但没有具体的可行解决方案,甚至在 CRM SDK 中也没有。我的 CRM 是本地 2013,插件是在沙盒隔离(无)之外注册的,WCF 服务使用命名域而不是 IP 地址,它通过 HTTP 协议运行,请参阅下面的代码。我已经满足了关于插件和外部系统的所有要求,但仍然没有运气。我还在控制台应用程序中测试了该服务,SOAP UI 工作正常,只是在插件中我遇到了问题。
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context == null)
{
throw new ArgumentNullException("loaclContext");
}
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity supplyClaimsEntity = (Entity)context.InputParameters["Target"];
if (supplyClaimsEntity.LogicalName != "new_supplierclaimsupdate")
{
return;
}
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);
string entityBeginUpload = "Start Upload";
try
{
BasicHttpBinding myBinding = new BasicHttpBinding();
myBinding.Name = "BasicHttpBinding_ISupplyClaimsService";
myBinding.Security.Mode = BasicHttpSecurityMode.None;
myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
myBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
myBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
EndpointAddress endPointAddress = new EndpointAddress(@"http://wmvapps01.tarsus.co.za/SupplyClaimsService.svc");
ChannelFactory<ISupplyClaimsService> factory = new ChannelFactory<ISupplyClaimsService>(myBinding, endPointAddress);
ISupplyClaimsService channel = factory.CreateChannel();
channel.StartApplication();
factory.Close();
}
【问题讨论】:
-
通常这种类型的错误是由于竞争条件和多线程造成的。您是否在插件的静态变量中存储了任何内容?
-
您是否在控制台应用程序中尝试过上述代码?
-
如果工厂或渠道支持IDisposible,也可以添加using语句。
标签: c# wcf dynamics-crm-2013