none
CRM 2011 Online plugin intermittently fails to call a remote web service using WebRequest

    Question

  • I am getting inconsistent behaviour when I attempt to call an external web service from within a CRM Online (2011) plugin.  The code pasted below uses a WebRequest to call a service (on an entity update)and process the response, fairly typical stuff for integrations. However, the service call does not always complete, and an Exception is thrown as in the error Trace below.  

    The Exception message is .Unable to connect to the remote server  even though the remote server is always available. I have implemented an example service at http://restservice2.appspot.com to simulate the issue.  The same issue occurs when I test with connections to production services, HTTP, HTTPS, authenticated and unauthenticated services.  

    Annoyingly, the issue does NOT occur in a CRM 2011 onpremise installation.

    I havent found anywhere that states this form of integration is not possible. Plugins running in the Sandbox (medium trust) have access to network resources and the plugin does successfully run about 50% of the time.

    I suspect that this is down to the persisted state of plugins within CRM and some resource not being cleaned up succesfully however I cannot track it down in the inline environment.

    Any help or pointers to a resolution would be gratefully appreciated

     

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occurred in the HttpWebRequestTest plug-in.Unable to connect to the remote server
      at System.Net.HttpWebRequest.GetResponse()
      at DealMakerServiceTestPlugin.HttpWebRequestTest.Execute(IServiceProvider serviceProvider)Detail:
    <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
     <ErrorCode>-2147220891</ErrorCode>
     <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
      <KeyValuePairOfstringanyType>
       <d2p1:key>OperationStatus</d2p1:key>
       <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value>
      </KeyValuePairOfstringanyType>
     </ErrorDetails>
     <Message>An error occurred in the HttpWebRequestTest plug-in.Unable to connect to the remote server
      at System.Net.HttpWebRequest.GetResponse()
      at DealMakerServiceTestPlugin.HttpWebRequestTest.Execute(IServiceProvider serviceProvider)</Message>
     <Timestamp>2011-03-21T12:19:37.3836882Z</Timestamp>
     <InnerFault i:nil="true" />
     <TraceText>
     
    [DealMakerServiceTestPlugin: DealMakerServiceTestPlugin.HttpWebRequestTest]
    [ac68fef8-b453-e011-957f-1cc1de08d711: DealMakerServiceTestPlugin.HttpWebRequestTest: Update of contact]
     
    Web service URL = http://restservice2.appspot.com/
    Building up the HttpWebRequest...
    Calling the web service..
    A general exception was caught: Unable to connect to the remote server
     
     
    </TraceText>
    </OrganizationServiceFault>
    

     

    Plugin code

    using System;
    using System.ServiceModel;
    using System.Net;
    using Microsoft.Xrm.Sdk; // CRM 2011: Microsoft.Crm.Sdk.IPlugin -> Microsoft.Xrm.Sdk.IPlugin
    using Microsoft.Xrm.Sdk.Query;
     
    namespace DealMakerServiceTestPlugin
    {
      /// <summary>
      /// This plugin performs an example call to a web service using the HttpWebRequest class.
      ///
      /// The purpose of this plugin is to demonstrate the inconsistency of external service calls in CRM online2
      /// </summary>
      public class HttpWebRequestTest : IPlugin
      {
        #region IPlugin Members
     
        public void Execute(IServiceProvider serviceProvider)
        {
          ITracingService tracer = null;
     
          HttpWebResponse ws = null;
          try
          {
            tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
     
            // Build up the service call url
            // This is an example service that returns a 201 and a couple of custom headers, exactly the same as the
            // live system that we are integrating with
            string url = @"http://restservice2.appspot.com/";
            string restAPI = "";
            string query = "";
     
            string webserviceURL = url + restAPI + query;
            tracer.Trace("Web service URL = {0}", webserviceURL);
     
            // Build up the request
            tracer.Trace("Building up the HttpWebRequest...");
            HttpWebRequest request = null;
            request = (HttpWebRequest)HttpWebRequest.Create(webserviceURL);
            // The production web service uses custom integration which is set with
            // request.Credentials = new System.Net.NetworkCredential(userid, password);
            // As this is a demonstration, this has been left out but does not influence the connection issue.
            request.UseDefaultCredentials = false;
            request.Method = "GET";
     
            //Get the response
            tracer.Trace("Calling the web service..");
            ws = (HttpWebResponse)request.GetResponse();
     
            // we expect an 201 (CREATED) and 2 custom headers
            tracer.Trace("Web service returned a Status Code of {0} and status description of {1}", ws.StatusCode, ws.StatusDescription);
            if (ws.StatusCode == HttpStatusCode.Created)
            {
              // all appears well, extract the header information
              string SSOToken = ws.Headers["X-TTG-SSO-TOKEN"].ToString();
              string Location = ws.Headers["Location"].ToString();
     
              tracer.Trace("Service call successful, token = {0}, location = {1}", SSOToken, Location);
            }
            else
            {
              string errmsg = String.Format("Unexpected response code of {0}, description {1}", ws.StatusCode, ws.StatusDescription);
              tracer.Trace(errmsg);
              // in this scenario, throw an exception because we want to stop the transaction
              throw new FaultException(errmsg);
            }
     
          }
          catch (FaultException<OrganizationServiceFault> ex)
          {
            tracer.Trace("A FaultException was caught: {0}", ex.Message);
            throw new InvalidPluginExecutionException(String.Concat("An error occurred in the HttpWebRequestTest plug-in.", ex.Message, Environment.NewLine, ex.StackTrace));
     
          }
          catch (Exception ex)
          {
            tracer.Trace("A general exception was caught: {0}", ex.Message);
            throw new InvalidPluginExecutionException(String.Concat("An error occurred in the HttpWebRequestTest plug-in.", ex.Message, Environment.NewLine, ex.StackTrace));
          }
          finally
          {
            // clean up the resources
            if (ws != null)
            {
              ws.Close();
            }
          }
        }
     
        #endregion
      }
    }
    

    Monday, March 21, 2011 12:48 PM

All replies

  • Update

    ------------

     

    I've had to raise an MS support ticket on this one so once I get a reply, Ill post it here.

     

    Richard


    Richard Barclay
    Monday, March 28, 2011 1:13 PM
  • Any update on this? 

    I have myself had the same issue, but the exception seems to be one of DNS resolution. I have also filed a support ticket and am waiting.

     

    Thx

    Vivek

    ==================

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Unexpected exception from plug-in (Execute): Microsoft.Crm.Sdk.Samples.TestPlugin: System.Net.WebException: The remote name could not be resolved: 'www.google.com'Detail: 

    <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">

      <ErrorCode>-2147220956</ErrorCode>

      <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />

      <Message>Unexpected exception from plug-in (Execute): Microsoft.Crm.Sdk.Samples.TestPlugin: System.Net.WebException: The remote name could not be resolved: 'www.google.com'</Message>

      <Timestamp>2011-04-04T17:08:17.6358923Z</Timestamp>

      <InnerFault i:nil="true" />

      <TraceText>

     

    [SamplePlugins: Microsoft.Crm.Sdk.Samples.TestPlugin]

    [980c1ffb-a15c-e011-940c-78e7d162dedb: Microsoft.Crm.Sdk.Samples.TestPlugin: Create of new_testentity]

     

     

    </TraceText>

    </OrganizationServiceFault>

    Monday, April 04, 2011 5:11 PM
  • Hi Vivek,

     

    My MS support request has been forwarded onto the platform team for investigation, but still no response.

     

    To get round the problem, I ended up creating an US region instance which works fine (thankfully it was only for a demo).  All EMEA instances I've created so far have had the same issue.

     

    Cheers

    Richard

     

     


    Richard Barclay
    Sunday, April 10, 2011 11:21 PM
  • Richard,

    Have you had any response from MS Support yet? I've come across the same issue with Web Service calls working perfectly on a US region instance, but not at all on a Singapore instance.

    Regards

    Chris Johnstone

    Monday, May 23, 2011 8:21 AM
  • Have you found a solution to your problem?

    Monday, December 05, 2011 12:51 PM
  • This problem was fixed a couple of months back. You should not be any more issues when accessing external services from sandbox plugins.
    Thursday, March 08, 2012 10:37 PM
  • This problem was fixed a couple of months back. You should not be any more issues when accessing external services from sandbox plugins.

    This is not true.  Having problems as of today so the problem is definitely not resolved.

    Why can't Microsoft get this right?

    Wednesday, April 11, 2012 4:53 AM
  • Is this problem fixed? Could someone point me how to access external SQL server from within CRM 2011 online plugin. 
    Tuesday, July 24, 2012 2:01 PM