Example #1
0
        public virtual OrganizationResponse Execute(OrganizationRequest request)
        {
            var requestDescription = GetRequestDescription(request);

            _controller.LogDetail("Executing crm request - " + requestDescription);

            OrganizationResponse result;

            try
            {
                result = Service.Execute(request);
            }
            catch (FaultException <OrganizationServiceFault> ex)
            {
                lock (_lockObject)
                {
                    //if we don't have a XrmConfiguration We Are Probably Inside A Transaction So Don't Bother Retry
                    if (XrmConfiguration == null)
                    {
                        throw;
                    }
                    //I have seen this error thrown when the sand box server is busy, and subsequent calls are successful. Going to add a retry
                    _controller.LogLiteral("Received FaultException<OrganizationServiceFault> retrying.. Details:" +
                                           ex.DisplayString());
                    Thread.Sleep(50);
                    result = Service.Execute(request);
                    _controller.LogLiteral("Successful retry");
                }
            }
            catch (CommunicationException ex)
            {
                lock (_lockObject)
                {
                    //Error was being thrown after service running overnight with no activity
                    //adding logic to reconnect when this error thrown
                    _controller.LogLiteral("Received " + ex.GetType().Name + " checking for Crm config to reconnect..");
                    if (XrmConfiguration != null)
                    {
                        _controller.LogLiteral("Crm config found attempting to reconnect..");
                        Service = XrmConnection.GetOrgServiceProxy(XrmConfiguration);
                        result  = Service.Execute(request);
                        _controller.LogLiteral("Reconnected..");
                    }
                    else
                    {
                        _controller.LogLiteral("No Crm config found unable to reconnect..");
                        throw;
                    }
                }
            }

            requestDescription = GetRequestDescription(request);
            _controller.LogDetail("Received crm response - " + requestDescription);

            return(result);
        }
Example #2
0
        public IOrganizationService GetOrganisationService(IXrmConfiguration xrmConfiguration)
        {
            if (!xrmConfiguration.UseXrmToolingConnector)
            {
                return(XrmConnection.GetOrgServiceProxy(xrmConfiguration));
            }
            else
            {
                if (string.IsNullOrWhiteSpace(xrmConfiguration.ToolingConnectionId))
                {
                    throw new Exception($"{nameof(IXrmConfiguration.ToolingConnectionId)} Is Required On The {nameof(IXrmConfiguration)} When {nameof(IXrmConfiguration.UseXrmToolingConnector)} Is Set");
                }
                lock (_lockObject)
                {
                    if (!_cachedToolingConnections.ContainsKey(xrmConfiguration.ToolingConnectionId))
                    {
                        var loginFrm = new ToolingConnectorForm(xrmConfiguration.ToolingConnectionId, xrmConfiguration.Name);
                        // Login process is Async, thus we need to detect when login is completed and close the form.
                        loginFrm.ConnectionToCrmCompleted += LoginFrm_ConnectionToCrmCompleted;
                        // Show the dialog here.
                        loginFrm.ShowDialog();

                        // If the login process completed, assign the connected service to the CRMServiceClient var
                        if (loginFrm.CrmConnectionMgr != null && loginFrm.CrmConnectionMgr.CrmSvc != null && loginFrm.CrmConnectionMgr.CrmSvc.IsReady)
                        {
                            _cachedToolingConnections.Add(xrmConfiguration.ToolingConnectionId, loginFrm.CrmConnectionMgr.CrmSvc);
                        }
                        else
                        {
                            throw new Exception("A Successful Connection Was Not Made By The Tooling Connector");
                        }
                    }
                    return(_cachedToolingConnections[xrmConfiguration.ToolingConnectionId]);
                }
            }
        }
        /// <summary>
        ///     Return Organisation Service Proxy
        /// </summary>
        /// <returns></returns>
        public static OrganizationServiceProxy GetOrgServiceProxy(IXrmConfiguration crmConfig)
        {
            var connection = new XrmConnection(crmConfig);

            return(connection.GetOrgServiceProxy());
        }