Пример #1
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         _organizationWebProxyClient?.Dispose();
         _organizationServiceContext?.Dispose();
         Trace.TraceInformation("WebProxyClient and ServiceContext disposed");
     }
 }
Пример #2
0
 void Dispose(bool disposing)
 {
     if (!disposedValue)
     {
         if (disposing)
         {
             if (CrmService != null)
             {
                 CrmService.Dispose();
             }
             if (OrganizationServiceContext != null)
             {
                 OrganizationServiceContext.Dispose();
             }
         }
         disposedValue = true;
     }
 }
Пример #3
0
 /// <summary>
 /// Dispose.
 /// </summary>
 /// <param name="disposing"></param>
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (OrganizationServiceProxy != null)
         {
             OrganizationServiceProxy.Dispose();
         }
         if (OrganizationServiceContext != null)
         {
             OrganizationServiceContext.Dispose();
         }
         if (organizationServiceProxy != null)
         {
             organizationServiceProxy.Dispose();
             organizationServiceProxy = null;
         }
         if (organizationServiceContext != null)
         {
             organizationServiceContext.Dispose();
             organizationServiceContext = null;
         }
     }
 }
Пример #4
0
        private void WaitForSystemJob(Guid systemJobId, int pollingInterval, int pollingTimeout, bool throwsException, int waitedFor = 0)
        {
            Logger.Trace(CultureInfo.InvariantCulture, TraceMessageHelper.EnteredMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name);

            if (Guid.Empty.Equals(systemJobId))
            {
                throw new ArgumentNullException(nameof(systemJobId));
            }
            if (pollingInterval < 1)
            {
                throw new PlatformException("Interval must be 1 second or more.");
            }
            pollingInterval *= 1000; pollingTimeout *= 1000; bool inProgress = true;

            while (inProgress)
            {
                try
                {
                    OrganizationServiceContext.ClearChanges();

                    SystemJob systemJob = CrmService.GetSystemJob(systemJobId);

                    if (systemJob == null)
                    {
                        throw new PlatformException("The requested job doesn't exist.");
                    }

                    switch (systemJob.StatusReason)
                    {
                    case SystemJobStatusReason.Succeeded:
                        inProgress = false;
                        break;

                    case SystemJobStatusReason.Canceling:
                    case SystemJobStatusReason.Canceled:
                    case SystemJobStatusReason.Failed:
                    case SystemJobStatusReason.Pausing:
                        if (throwsException)
                        {
                            throw new PlatformException(string.Format(CultureInfo.InvariantCulture, "The requested job failed. {0} {1}", systemJob.StatusReason, systemJob.Message));
                        }
                        inProgress = false;
                        break;
                    }
                }
                catch (MessageSecurityException ex)
                {
                    Logger.Warn(CultureInfo.InvariantCulture, "Oups ... a security exception occurred, we will try to reconnect.");
                    Logger.Warn(ex.Message);

                    Connection.SetOrganizationService();

                    OrganizationServiceContext.Dispose();
                    OrganizationServiceContext = new CrmServiceContext(Connection.OrganizationService);

                    CrmService.Dispose();
                    CrmService = new CrmService(OrganizationServiceContext, Connection);

                    WaitForSystemJob(systemJobId, pollingInterval / 1000, pollingTimeout / 1000, throwsException, waitedFor);
                    break;
                }
                catch (FaultException <OrganizationServiceFault> ex)
                {
                    if (ex.Detail != null && ex.Detail.ErrorCode == -2147020463)
                    {
                        Logger.Warn(CultureInfo.InvariantCulture, "Oups ... another solution is currently installing, we will retry in a minute.");
                        Logger.Warn(ex.Detail.Message);

                        Thread.Sleep(60000); WaitForSystemJob(systemJobId, pollingInterval, pollingTimeout, throwsException);
                    }
                    else
                    {
                        throw;
                    }
                }

                if (waitedFor > pollingTimeout)
                {
                    if (throwsException)
                    {
                        throw new TimeoutException(string.Format(CultureInfo.InvariantCulture, "The system job {0} timed out.", systemJobId));
                    }
                    inProgress = false;
                }

                Logger.Info(CultureInfo.InvariantCulture, "The system job is running for {0} seconds.", waitedFor / 1000);
                Thread.Sleep(pollingInterval); waitedFor += pollingInterval;
            }

            Logger.Trace(CultureInfo.InvariantCulture, TraceMessageHelper.ExitingMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name);
        }
Пример #5
0
        private void WaitForImportJob(Guid importJobId, Guid systemJobId, int pollingInterval, int pollingTimeout, bool throwsException, int waitedFor = 0)
        {
            Logger.Trace(CultureInfo.InvariantCulture, TraceMessageHelper.EnteredMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name);

            if (Guid.Empty.Equals(systemJobId))
            {
                throw new ArgumentNullException(nameof(systemJobId));
            }
            if (pollingInterval < 1)
            {
                throw new PlatformException("Interval must be 1 second or more.");
            }
            pollingInterval *= 1000; pollingTimeout *= 1000; bool inProgress = true;

            while (inProgress)
            {
                try
                {
                    OrganizationServiceContext.ClearChanges();

                    SystemJob systemJob = CrmService.GetSystemJob(systemJobId);

                    if (systemJob == null)
                    {
                        throw new PlatformException("The requested job doesn't exist.");
                    }

                    switch (systemJob.StatusReason)
                    {
                    case SystemJobStatusReason.Succeeded:
                        inProgress = false;
                        break;

                    case SystemJobStatusReason.Canceling:
                    case SystemJobStatusReason.Canceled:
                    case SystemJobStatusReason.Failed:
                    case SystemJobStatusReason.Pausing:
                        if (throwsException)
                        {
                            throw new PlatformException(string.Format(CultureInfo.InvariantCulture, "The requested job failed. {0} {1}", systemJob.StatusReason, systemJob.Message));
                        }
                        inProgress = false;
                        break;
                    }

                    ImportJob importJob = CrmService.GetImportJob(importJobId);

                    if (importJob != null && importJob.Progress != null)
                    {
                        int progress = Convert.ToInt32(importJob.Progress.GetValueOrDefault(0));
                        Logger.Info(CultureInfo.InvariantCulture, "Importing solution ... progress: {0}%", progress);
                        if (progress >= 100)
                        {
                            if (!string.IsNullOrWhiteSpace(importJob.Data))
                            {
                                IEnumerable <ImportJobResult> importJobResults = ParseImportJobXmlData(importJob.Data);

                                int warningCount = 0; int errorCount = 0; int fatalErrorCount = 0;

                                foreach (ImportJobResult importJobResult in importJobResults)
                                {
                                    string name      = GetNameText(importJobResult);
                                    string processed = GetProcessedText(importJobResult);
                                    string result    = GetResultText(importJobResult);

                                    switch (importJobResult.Result)
                                    {
                                    case ImportJobStatus.Warning:
                                        Logger.Warn(CultureInfo.InvariantCulture, "{0}{1}{2}{3}", importJobResult.ElementName, name, processed, result);
                                        warningCount++;
                                        break;

                                    case ImportJobStatus.Error:
                                        Logger.Error(CultureInfo.InvariantCulture, "{0}{1}{2}{3}", importJobResult.ElementName, name, processed, result);
                                        if (importJobResult.ErrorCode != "0")
                                        {
                                            errorCount++;
                                        }
                                        break;

                                    case ImportJobStatus.Failure:
                                        Logger.Fatal(CultureInfo.InvariantCulture, "{0}{1}{2}{3}", importJobResult.ElementName, name, processed, result);
                                        if (importJobResult.ErrorCode != "0")
                                        {
                                            fatalErrorCount++;
                                        }
                                        break;

                                    default:
                                        Logger.Info(CultureInfo.InvariantCulture, "{0}{1}{2}{3}", importJobResult.ElementName, name, processed, result);
                                        break;
                                    }
                                }

                                if (errorCount > 0 || fatalErrorCount > 0)
                                {
                                    throw new PlatformException("One or more error(s) occurred whilst importing the solution.");
                                }
                            }

                            inProgress = false;
                        }
                    }
                }
                catch (MessageSecurityException ex)
                {
                    Logger.Warn(CultureInfo.InvariantCulture, "Oups ... a security exception occurred, we will try to reconnect.");
                    Logger.Warn(ex.Message);

                    Connection.SetOrganizationService();

                    OrganizationServiceContext.Dispose();
                    OrganizationServiceContext = new CrmServiceContext(Connection.OrganizationService);

                    CrmService.Dispose();
                    CrmService = new CrmService(OrganizationServiceContext, Connection);

                    WaitForImportJob(importJobId, systemJobId, pollingInterval / 1000, pollingTimeout / 1000, throwsException, waitedFor);
                    break;
                }
                catch (FaultException <OrganizationServiceFault> ex)
                {
                    if (ex.Detail != null && ex.Detail.ErrorCode == -2147020463)
                    {
                        Logger.Warn(CultureInfo.InvariantCulture, "Oups ... another solution is currently installing, we will retry in a minute.");
                        Logger.Warn(ex.Detail.Message);

                        Thread.Sleep(60000); WaitForImportJob(importJobId, systemJobId, pollingInterval, pollingTimeout, throwsException);
                    }
                    else
                    {
                        throw;
                    }
                }

                if (waitedFor > pollingTimeout)
                {
                    if (throwsException)
                    {
                        throw new TimeoutException(string.Format(CultureInfo.InvariantCulture, "The system job {0} timed out.", systemJobId));
                    }
                    inProgress = false;
                }

                Thread.Sleep(pollingInterval); waitedFor += pollingInterval;
            }

            Logger.Trace(CultureInfo.InvariantCulture, TraceMessageHelper.ExitingMethod, SystemTypeName, MethodBase.GetCurrentMethod().Name);
        }
Пример #6
0
 public void Dispose()
 {
     _organizationWebProxyClient?.Dispose();
     _organizationServiceContext?.Dispose();
     Trace.TraceInformation("WebProxyClient and ServiceContext disposed");
 }
Пример #7
0
 public void Dispose()
 {
     context.Dispose();
 }
Пример #8
0
 public void Dispose()
 {
     service.Dispose();
 }
Пример #9
0
        public void CaseDeletion(IOrganizationService _orgServ)
        {
            Console.WriteLine("Starting Case Deletion");
            OrganizationServiceContext _orgContext = new OrganizationServiceContext(_orgServ);
            Incident myCase     = null;
            int      pageNo     = 1;
            int      record     = 0;
            string   casTickNum = "";
            string   casType;
            Guid     last  = new Guid();
            Guid     first = new Guid();

start:
            EntityCollection retrievedCases = GetCases(_orgServ, pageNo, casTickNum, first, last);

            if (retrievedCases.Entities.Count > 1)
            {
                Console.WriteLine("Cases Retrieved");

                for (int r = 0; r < retrievedCases.Entities.Count; r++)
                {
                    record++;
                    Console.WriteLine("record number = " + record);
                    myCase = retrievedCases[r].ToEntity <Incident>();
                    var casId = myCase.IncidentId;

                    if (myCase.gcs_CaseTypes == null)
                    {
                        _orgContext.Attach(myCase);
                        _orgContext.DeleteObject(myCase);
                        _orgContext.Dispose();
                        continue;
                    }
                    casType = myCase.gcs_CaseTypes.Name.ToString();

                    CaseSaveList((Guid)casId, casType);

                    int idExist = safeList.FindIndex(s => s.caseGuid == casId);

                    try
                    {
                        if (idExist >= 0)
                        {
                            Entity updateCase = new Entity();
                            updateCase.LogicalName = myCase.LogicalName;
                            updateCase.Attributes["shg_casesavefield"] = true;
                            updateCase.Id = myCase.Id;
                            if (myCase.StateCode != 0)
                            {
                                SetStateRequest setStateRequest = new SetStateRequest()
                                {
                                    EntityMoniker = new EntityReference
                                    {
                                        Id          = myCase.Id,
                                        LogicalName = myCase.LogicalName,
                                    },
                                    Status = new OptionSetValue(1),
                                    State  = new OptionSetValue(0)
                                };
                                _orgServ.Execute(setStateRequest);
                            }
                            _orgServ.Update(updateCase);
                            continue;
                        }
                    }
                    catch (FaultException <OrganizationServiceFault> e)
                    {
                        Console.WriteLine(e + "case which failed = " + myCase.TicketNumber + " case type = " + myCase.gcs_CaseTypes);
                        continue;
                    }
                }
                pageNo++;
                first = (Guid)retrievedCases.Entities.Select(s => s.Attributes["incidentid"]).First();
                last  = (Guid)retrievedCases.Entities.Select(s => s.Attributes["incidentid"]).Last();
                goto start;
            }
            else
            {
                BulkCaseDeletion(_orgServ);
                BulkActivityDeletion(_orgServ);
            }
        }