none
MS Dynamics CRM Plugin Error - System.Net.WebException: The request was aborted: The operation has timed out. RRS feed

  • General discussion

  • We are using custom Dynamics CRM Plugin C# with our Web API to import Accounts/Contracts, whenever our Client's users Add/Update/Delete on Accounts/Contracts. Now sometimes we are getting exceptions randomly from past two months while Client's users started more Add/Update on Accounts/Contracts. The error is given below:

    Plugin Custom Tracing  EntityID: 886f371a-5e55-e811-813f-e0071b6af251  

    WebAPIError| System.Net.WebException:The request was aborted:The operation has timed out.     at System.Net.HttpWebRequest.GetResponse()    

    at ACRMPlugin.Plugin.APIHit(String apiUri, Byte[] data, String method, String entityType, String otherInfo)

    We tried to resolve it but no luck yet. Please analyse this. After that you can reply me via email/number. Our Plugin code is given below:

    Our Web API Request Method:

           public string APIHit(string apiUri, byte[] data = null, string method = "Get", string entityType = "", string otherInfo = "")

            {

                try

                {

                    var result = "";

                    using (HttpWebResponse rspnsCld = CreateApiRequest(apiUri, data, method).GetResponse() as HttpWebResponse)

                    {

                        using (var responseStream = new StreamReader(rspnsCld.GetResponseStream()))

                        {

                            result = responseStream.ReadToEnd();

                        }

                    }

                    return result;

               }

                catch (WebException ex)

                {

                    tracingServiceApi.Trace(" WebAPIError| {0}", ex.ToString() + " |" + otherInfo); //Do not change any space

                    return "exception";

                }

                catch (Exception ex)

                {

                    tracingServiceApi.Trace(" WebAPIError| {0}", ex.ToString() + " |" + otherInfo); //Do not change any space

                    return "exception";

                }

            }

            public WebRequest CreateApiRequest(string resoucre, byte[] data = null, string method = "Get")

            {

                string ContentType = "application/json";

                var request = WebRequest.Create(resoucre);

                request.Method = method;

                request.ContentType = ContentType;

                request.Headers.Add("ApiKey", APIKey);

                request.Headers.Add("UserToken", Token);

                request.Timeout = 1200000;

                if ((method == "PUT" || method == "Post") && data != null)

                {

                    request.ContentLength = data.Length;

                    Stream stream = request.GetRequestStream();

                    stream.Write(data, 0, data.Length);

                    stream.Close();

                }

                return request;

            }

    Our Plugin Update Client/Account Method:

            public string UpdateClient(Entity clientEntity)

            {

               string ApiUriUpdateClient = apiLink + "/Client/Post";

                string dotNetAPILink = apiLinkDotNet + "/Client/Post";

                try

                {

                   TalygenClient client = new TalygenClient();

                    client.CompanyName = clientEntity.Contains("name") ? Convert.ToString(clientEntity["name"]) : null;

                    client.ClientEmail = clientEntity.Contains("emailaddress1") ? Convert.ToString(clientEntity["emailaddress1"]) : null;

                    client.AddressLine1 = clientEntity.Contains("address1_line1") ? Convert.ToString(clientEntity["address1_line1"]) : null;

                    client.AddressLine2 = clientEntity.Contains("address1_line2") ? Convert.ToString(clientEntity["address1_line2"]) : null;

                    client.AddressLine3 = clientEntity.Contains("address1_line3") ? Convert.ToString(clientEntity["address1_line3"]) : null;

                    client.City = clientEntity.Contains("address1_city") ? Convert.ToString(clientEntity["address1_city"]) : null;

                    client.StateOrProvience = clientEntity.Contains("address1_stateorprovince") ? Convert.ToString(clientEntity["address1_stateorprovince"]) : null;

                    client.ZipCode = clientEntity.Contains("address1_postalcode") ? Convert.ToString(clientEntity["address1_postalcode"]) : null;

                    client.ContactNumber = clientEntity.Contains("telephone1") ? Convert.ToString(clientEntity["telephone1"]) : null;

                    client.Fax = clientEntity.Contains("fax") ? Convert.ToString(clientEntity["fax"]) : null;

                    client.RefID = Convert.ToString(clientEntity["accountid"]);

                    client.RefName = "MS Dynamics CRM";

                    EntityReference contactRefId = clientEntity.Contains("primarycontactid") ? (EntityReference)clientEntity.Attributes["primarycontactid"] : null;

                    client.ContactRefId = contactRefId != null ? Convert.ToString(contactRefId.Id) : null;

                    EntityReference parentRefId = clientEntity.Contains("parentaccountid") ? (EntityReference)clientEntity.Attributes["parentaccountid"] : null;

                    client.ParentRefId = parentRefId != null ? Convert.ToString(parentRefId.Id) : null;

                    client.AccountNumber = clientEntity.Contains("accountnumber") ? Convert.ToString(clientEntity["accountnumber"]) : null;

                    client.StatusCode = clientEntity.Contains("statuscode") ? Convert.ToString(((OptionSetValue)clientEntity.Attributes["statuscode"]).Value) : null;

                    string postData = string.Format(@"{{""ClientEmail"":""{0}"",""AddressLine1"":""{1}"",""AddressLine2"":""{2}"",

                        ""City"":""{3}"",

                        ""StateOrProvience"":""{4}"",

                        ""ZipCode"":""{5}"",

                        ""Country"":""{6}"",

                        ""RefID"":""{7}"",

                        ""RefName"":""{8}"",

                        ""ContactRefId"":""{9}"",

                        ""CompanyName"":""{10}"",

                        ""ContactNumber"":""{11}"",

                        ""Fax"":""{12}"",

                        ""ParentRefId"":""{13}"",

                        ""AccountNumber"":""{14}"",

                        ""StatusCode"":""{15}""}}", client.ClientEmail, client.AddressLine1, client.AddressLine2 + " " + client.AddressLine3, client.City, client.StateOrProvience, client.ZipCode,

                                                  client.Country, client.RefID, client.RefName, client.ContactRefId,

                                                  !string.IsNullOrWhiteSpace(client.CompanyName) ? Regex.Replace(client.CompanyName, regFull, "").Trim() : client.CompanyName, //rgx.Replace(client.CompanyName, "")

                                                  client.ContactNumber, client.Fax, client.ParentRefId,

                                               client.AccountNumber, client.StatusCode);

                    byte[] byteArray = Encoding.UTF8.GetBytes(postData);

                    string otherInfo = "Account Name^" + client.CompanyName + "@Account Number^" + client.AccountNumber + "@Account Guid^" + client.RefID; //Do not change anything in this string. it is used in CRM main utility code to extract info

                    

                    //Stage Thread

                    if (isSendToStage)

                    {

                        ThreadStart starterStg = () => CallStageAPI(dotNetAPILink, byteArray, otherInfo);

                        Thread threadStg = new Thread(starterStg);

                        threadStg.Start();

                    }

                   Thread.Sleep(45000);

                    return APIHit(ApiUriUpdateClient, byteArray, "Post", "Account", otherInfo + " [Cloud]");

                }

                catch (Exception Ex)

                {

                    return ("Error On Update Client : " + Ex.ToString());

                }

            }

            public void CallStageAPI(string dotNetAPILink, byte[] byteArray, string otherInfo)

            {

                Thread.Sleep(45000);

                StageAPIHit(dotNetAPILink, byteArray, "Post", "Account", otherInfo + " [Stage]");

                Thread.Sleep(500);

            }

    Please provide me the root cause & solution on this critical issue asap.


    • Edited by ArvSha Monday, June 25, 2018 7:08 PM
    Friday, June 22, 2018 3:57 PM