本人微信公众号:微软动态CRM专家罗勇 ,回复298或者20190120可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me 。
系统作业(逻辑名称asyncoperation)这个实体存储了工作流、异步SDK插件步骤的运行记录,若是不及时删除的话,这个实体记录数太多会严重影响系统性能。
所以我们一般做法分成两种,一个是建立一个系统批量删除作业,删除状态为成功、失败、已取消,且创建日期为X个月之前的记录(因为不少公司对日志保留有要求,起码保留一个月),这个批量删除作业最频繁可以每隔七天运行一次。
另外一个就是切记两个选项要选中,一个是工作流的【自动删除已完成的工作流作业(以节省磁盘空间)】要选中。
另一个就是SDK插件步骤的【Delete AsyncOperation if StatusCode = Successful】要选中。
这两个选项如果不选中,有时候会带来严重问题,比如说SDK插件步骤,注册在所有实体的RetrieveMutiple,Retrieve等运行非常频繁的消息上,那带来的系统作业记录会非常多,可能一天几百万。
难道每次都一个个用眼睛看手工检查,太Low,我们当然有程序办法,跑一下程序就可以检查出来,下面就是可以参考的代码。
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")); Console.WriteLine(ent.Id); } } } 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 }); } } }