Dynamics 365的系统作业实体记录太多怎么办?

摘要: 本人微信公众号:微软动态CRM专家罗勇 ,回复294或者20190111可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me 。 

这个一般是工作流没有选中【自动删除已完成的工作流作业(以节省磁盘空间)】这个选项,或者是SDK插件步骤没有选中【Delete AsyncOperation if StatusCode = Successful】这个选项。特别是运行频繁的工作流,或者注册的频繁执行的异步执行的SDK插件步骤(如注册在实体上的Retrieve 或者 RetrieveMultiple 消息的插件步骤),如果没有设置对,会导致【系统作业】实体(实体逻辑名称asyncoperation)的记录增长的非常快,占用磁盘会非常大。

所以我开发了一个程序来检查是否有这样的插件步骤或者工作流,不多说了,上代码。

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Configuration;
using System.Net;
using System.ServiceModel.Description;

namespace CheckWorkflowPluginStepAutoDelete
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string inputKey;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
                IServiceManagement<IOrganizationService> orgServiceMgr = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(ConfigurationManager.AppSettings["orgUrl"]));
                AuthenticationCredentials orgAuCredentials = new AuthenticationCredentials();
                orgAuCredentials.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings["userName"];
                orgAuCredentials.ClientCredentials.UserName.Password = ConfigurationManager.AppSettings["passWord"];
                string needConfirm = ConfigurationManager.AppSettings["needConfirm"];
                using (var orgSvc = GetProxy<IOrganizationService, OrganizationServiceProxy>(orgServiceMgr, orgAuCredentials))
                {
                    orgSvc.Timeout = new TimeSpan(8, 0, 0);
                    WhoAmIRequest whoReq = new WhoAmIRequest();
                    var whoRsp = orgSvc.Execute(whoReq) as WhoAmIResponse;
                    var userEntity = orgSvc.Retrieve("systemuser", whoRsp.UserId, new Microsoft.Xrm.Sdk.Query.ColumnSet("fullname"));
                    Console.WriteLine(string.Format("欢迎【{0}】登陆到【{1}】", userEntity.GetAttributeValue<string>("fullname"), ConfigurationManager.AppSettings["orgUrl"]));
                    Console.WriteLine("本程序用于检查工作流/SDK插件步骤是否选中了【运行成功后自动删除日志】!");
                    if (needConfirm == "Y")
                    {
                        Console.WriteLine("当前处于需要确认才会继续的模式,若要继续请输入Y然后回车确认!");
                        inputKey = Console.ReadLine();
                        if (inputKey.ToUpper() == "Y")
                        {
                            CheckSDKMessageProcessingStepAutoDelete(orgSvc);
                            CheckWorkflowAutoDelete(orgSvc);
                        }
                        else
                        {
                            Console.WriteLine("你选择了取消运行!");
                        }
                    }
                    else
                    {
                        CheckSDKMessageProcessingStepAutoDelete(orgSvc);
                        CheckWorkflowAutoDelete(orgSvc);
                    }
                }
                Console.Write("程序运行完成,按任意键退出." + DateTime.Now.ToString());
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("程序运行出错:" + ex.Message + ex.StackTrace);
                Console.ReadLine();
            }
        }

        private static void CheckSDKMessageProcessingStepAutoDelete(OrganizationServiceProxy orgSvc)
        {
            const string functionName = "检查SDK插件步骤是否选中了【运行成功后自动删除日志】";
            Console.WriteLine(string.Format("开始 {0} - {1}", functionName, DateTime.Now.ToString()));
            try
            {
                QueryExpression qe = new QueryExpression("sdkmessageprocessingstep");
                qe.ColumnSet = new ColumnSet("name");
                qe.NoLock = true;
                qe.Criteria.AddCondition(new ConditionExpression("mode", ConditionOperator.Equal, 1));
                qe.Criteria.AddCondition(new ConditionExpression("asyncautodelete", ConditionOperator.Equal, false));
                qe.Criteria.AddCondition(new ConditionExpression("iscustomizable", ConditionOperator.Equal, true));
                EntityCollection ec = orgSvc.RetrieveMultiple(qe);
                if (ec.Entities.Count == 0)
                {
                    Console.WriteLine("Perfect!所有SDK插件步骤都选中了成功后自动删除运行日志!");
                }
                else
                {
                    Console.WriteLine("所有异步运行的SDK插件步骤没有选中【运行成功后自动删除日志】清单如下:");
                    foreach (Entity ent in ec.Entities)
                    {
                        Console.WriteLine(ent.GetAttributeValue<string>("name"));
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("运行 {0} 出现异常:{1}", functionName, ex.Message + ex.StackTrace));
            }
            Console.WriteLine(string.Format("结束 {0} - {1}", functionName, DateTime.Now.ToString()));
            Console.WriteLine("================================================");
        }

        private static void CheckWorkflowAutoDelete(OrganizationServiceProxy orgSvc)
        {
            const string functionName = "检查工作流是否选中了【运行成功后自动删除日志】";
            Console.WriteLine(string.Format("开始 {0} - {1}", functionName, DateTime.Now.ToString()));
            try
            {
                var fetchXml = @"<fetch version='1.0' mapping='logical' distinct='false' no-lock='true'>
  <entity name='workflow'>
    <attribute name='name' />
    <filter type='and'>
      <condition attribute='type' operator='eq' value='1' />
      <condition attribute='category' operator='eq' value='0' />
      <condition attribute='statecode' operator='eq' value='1' />
      <condition attribute='asyncautodelete' operator='ne' value='1' />
      <condition attribute='mode' operator='eq' value='0' />
    </filter>
  </entity>
</fetch>";
                var workflowEntities = orgSvc.RetrieveMultiple(new FetchExpression(fetchXml));
                if (workflowEntities.Entities.Count == 0)
                {
                    Console.WriteLine("Perfect!所有工作流都选中了成功后自动删除运行日志!");
                }
                else
                {
                    Console.WriteLine("所有异步运行的工作流没有选中【运行成功后自动删除日志】清单如下:");
                    foreach (var item in workflowEntities.Entities)
                    {
                        Console.WriteLine(item.GetAttributeValue<string>("name"));
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("运行 {0} 出现异常:{1}", functionName, ex.Message + ex.StackTrace));
            }
            Console.WriteLine(string.Format("结束 {0} - {1}", functionName, DateTime.Now.ToString()));
            Console.WriteLine("================================================");
        }

        private static TProxy GetProxy<TService, TProxy>(
IServiceManagement<TService> serviceManagement,
AuthenticationCredentials authCredentials)
            where TService : class
            where TProxy : ServiceProxy<TService>
        {
            Type classType = typeof(TProxy);

            if (serviceManagement.AuthenticationType !=
                AuthenticationProviderType.ActiveDirectory)
            {
                AuthenticationCredentials tokenCredentials =
                    serviceManagement.Authenticate(authCredentials);
                return (TProxy)classType
                    .GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(SecurityTokenResponse) })
                    .Invoke(new object[] { serviceManagement, tokenCredentials.SecurityTokenResponse });
            }
            return (TProxy)classType
                .GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(ClientCredentials) })
                .Invoke(new object[] { serviceManagement, authCredentials.ClientCredentials });
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/luoyong0201/p/Dynamics_365_System_Job_Auto_Deletion.html