Dynamics 365检查工作流、SDK插件步骤是否选中运行成功后自动删除系统作业记录
2021-03-20 22:08
387 查看
本人微信公众号:微软动态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 }); } } }
相关文章推荐
- input 的属性autocomplete 默认为on 其含义代表是否让浏览器自动记录之前输入的值 很多时候,需要对客户的资料进行保密,防止浏览器软件或者恶意插件获取到 可以在input中加入a
- 怎么删除访问windows共享时,系统自动记录的用户名和密码
- 解决“火狐开启自动检查插件是否匹配“”去除方法
- 确定系统中是否存在绑定变量的情况,ASKTOM提供函数remove_constans()来检查共享池中的SQL运行情况
- 检查你的系统是否能运行Windows7
- 机房收费系统之选中MSHFlexGrid中的某条记录进行删除
- JQuery form表单提交前验证单选框是否选中、删除记录时验证经验总结(整理)
- 页面上有个添加按钮,程序运行成功的,当我添加一条记录后刷新页面,它自动又添加一条,这样怎么修改?
- vs2019,“发生生成错误,是否继续并运行上次的成功的生成?”无法启动程序,系统找不到指定的文件
- 转载:用oralce连接.net客户端出现问题:“数据连接不成功,请检查该数据库是否已启动尝试加载oracle客户端时引发BadImageFormatException.如果在安装32位Oracle客户端组件的情况下以64位模式运行,”的解
- 部署步骤“回收 IIS 应用程序池”中出现错误: 本地 SharePoint 服务器不可用。请检查该服务器是否正在运行并连接到 SharePoint 服务器场。
- Bucket不为空,请检查该Bucket是否包含未删除的Object或者未成功的Multipart碎片
- 步骤四_linux下的fcitx的安装_系统起动时自动运行
- windows 系统 安装 net-snmp 并成功运行命令的步骤
- win 系统设置weblogic 进行定时自动重启并删除其日志和缓存文件,定时监控cpu是否达到100%并重启weblogic服务
- 对系统调用和内存分配检查是否执行成功
- 定时检查网站是否运行正常,自动重启TOMCAT的简单方法
- 使用JDK工具检查运行系统是否存在内存泄露
- 用oralce连接.net客户端出现问题:“数据连接不成功,请检查该数据库是否已启动尝试加载oracle客户端时引发BadImageFormatException.如果在安装32位Oracle客户端组件的情况下以64位模式运行,”的解决办法
- [SQL] 如何在SQL Server2005数据库中检查一个表是否存在,如存在就删除表记录,如不存在就建表.