1
votes

Hi I have a CRM 2013 Plugin which calls a WCF service, the service fails with the following error:

'The communication object, System.ServiceModel.ChannelFactory`1[ISupplyClaimsService], cannot be modified while it is in the Opening state',

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? I see some posts on it online, but no concrete working solution is out there, not even in the 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. 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.

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();


            }
1
Normally this type of error is due to a race condition and multi threading. Are you storing anything in static variables in your Plugin?Daryl
Have you tried the above code in a console application?Rickard N
You could also add a using statement if the factory or channel support IDisposible.Daryl

1 Answers

1
votes

I have a WCF Rest WebService that have a Rest method. WebService code:

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:

<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:

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;

}