简体   繁体   English

CRM Dynamics调用WCF服务错误

[英]CRM Dynamics Calling WCF Service Error

Hi I have a CRM 2013 Plugin which calls a WCF service, the service fails with the following error: 嗨,我有一个调用WCF服务的CRM 2013插件,该服务失败并出现以下错误:

'The communication object, System.ServiceModel.ChannelFactory`1[ISupplyClaimsService], cannot be modified while it is in the Opening state', “通讯对象System.ServiceModel.ChannelFactory`1 [ISupplyClaimsService]处于打开状态时无法修改”,

I also sometimes get that when the call is made to the service, the Plugin Registration tool crashes. 当调用服务时,有时我还会收到“插件注册”工具崩溃的消息。 Is is possible to call a WCF service from a Plugin? 是否可以从插件调用WCF服务? I see some posts on it online, but no concrete working solution is out there, not even in the CRM SDK. 我在线上看到了一些帖子,但是没有具体的工作解决方案,即使在CRM SDK中也没有。 My CRM is on-premises 2013, plugin is registered out of Sandbox isolation(NONE), The WCF Service uses a named domain and not an IP address, its runs through HTTP protocol, Please see my code below. 我的CRM于2013年部署,插件已通过沙盒隔离(NONE)注册,WCF服务使用命名域而不是IP地址,它通过HTTP协议运行,请参阅下面的代码。 I have met all requirements with regards to plugins and external systems, but still no luck. 我已经满足了有关插件和外部系统的所有要求,但还是没有运气。 I have also tested the service in a Console application,SOAP UI it works fine, just in the Plugin I am having issues. 我还在控制台应用程序中测试了该服务,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();


            }

I have a WCF Rest WebService that have a Rest method . 我有一个具有Rest methodWCF Rest WebService WebService code: WebService代码:

public interface ICRMRestWebService
{
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,    
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "GetLatestContractByContactIdRestMethod")]
LatestMembershipResponse GetLatestContractByContactIdRestMethod(string contactId, AuthenticateRequest authenticateRequest);
}

Web.config of WCF Rest WebService is : WCF Rest WebService is Web.config WCF Rest WebService is

<system.serviceModel>
<services>
  <service name="GRID.CRM.WebServices.CRMRestWebService.CRMRestWebService" behaviorConfiguration="ServiceBehaviour">
    <endpoint address=""  binding="webHttpBinding" contract="GRID.CRM.WebServices.CRMRestWebService.ICRMRestWebService" behaviorConfiguration="web">
    </endpoint>
  </service>
</services>
<bindings />
<client />
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehaviour">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />

I call this method from CRM Plugin like below: 我从CRM Plugin调用此方法,如下所示:

public void Execute(IServiceProvider serviceProvider)
  {
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    IOrganizationServiceFactory serviceFactory;
    IOrganizationService service;
    if (context.Depth > 1)
            return;

    if (context.InputParameters.Contains("Target"))
    {
      //Create Service from Service Factory
     serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
     service = serviceFactory.CreateOrganizationService(context.UserId);

     var javaScriptSerializer = new JavaScriptSerializer();
     javaScriptSerializer.MaxJsonLength = 104857600; //200 MB unicode

            StringBuilder URI = new StringBuilder();
            URI.Append(crmRestWebServiceUrl).Append(webServiceMethod);
            //Logger.Info("GetLatestMembershipFromCRMRestWebService is called with Url: " + URI.ToString());
            var request = (HttpWebRequest)WebRequest.Create(URI.ToString());
            request.Method = "POST";
            request.Accept = "application/json";
            request.ContentType = "application/json; charset=utf-8";

            //Serialize request object as JSON and write to request body
            if (latestMembershipRequest != null)
            {
                var stringBuilder = new StringBuilder();
                javaScriptSerializer.Serialize(latestMembershipRequest, stringBuilder);
                var requestBody = stringBuilder.ToString();
                request.ContentLength = requestBody.Length;
                var streamWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
                streamWriter.Write(requestBody);
                streamWriter.Close();

               // Logger.Info("Request Object that will be sent is: " + stringBuilder.ToString());
            }

           // Logger.Info("Sending Request to CRMRestWebService to get LatestMembership.");
            var response = request.GetResponse();

            //Read JSON response stream and deserialize
            var streamReader = new System.IO.StreamReader(response.GetResponseStream());
            var responseContent = streamReader.ReadToEnd().Trim();
            LatestMembershipResponse latestMembershipResponse = javaScriptSerializer.Deserialize<LatestMembershipResponse>(responseContent);
           // Logger.Info("Latest Membership Reveived is: " + responseContent.ToString() + " has been deserialized Successfully.");
            return latestMembershipResponse;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM