/// <summary> /// 执行插件方法 /// </summary> /// <param name="action">Action实体</param> /// <param name="delegateService">委托服务</param> private static void ExecuteCSharpLibraryMethod(ActionEntity action, IDelegateService delegateService) { try { //取出当前应用程序执行路径 var methodInfo = action.MethodInfo; var assemblyFullName = methodInfo.AssemblyFullName; var methodName = methodInfo.MethodName; var executingPath = ConfigHelper.GetExecutingDirectory(); var pluginAssemblyName = string.Format("{0}\\{1}\\{2}.dll", executingPath, "plugin", assemblyFullName); var pluginAssemblyTypes = Assembly.LoadFile(pluginAssemblyName).GetTypes(); Type outerClass = pluginAssemblyTypes .Single(t => !t.IsInterface && t.FullName == methodInfo.TypeFullName); //object instance = Activator.CreateInstance(outerClass, parameters.ConstructorParameters); object instance = Activator.CreateInstance(outerClass); System.Reflection.MethodInfo mi = outerClass.GetMethod(methodName); object[] methodParams = null; if (!string.IsNullOrEmpty(action.Arguments.Trim())) { methodParams = CompositeParameterValues(action.Arguments, delegateService); } var result = mi.Invoke(instance, methodParams); } catch (System.Exception ex) { throw new WorkflowException(string.Format("执行C#组件方法出错:{0}", ex.Message)); } }
/// <summary> /// 执行外部服务实现类 /// </summary> /// <param name="action">操作</param> /// <param name="delegateService">委托服务类</param> private static void Execute(ActionEntity action, IDelegateService delegateService) { if (action.ActionType == ActionTypeEnum.Event) { if (action.ActionMethod == ActionMethodEnum.LocalService) { ExecuteLocalService(action, delegateService); } else if (action.ActionMethod == ActionMethodEnum.CSharpLibrary) { ExecuteCSharpLibraryMethod(action, delegateService); } else if (action.ActionMethod == ActionMethodEnum.WebApi) { ExecuteWebApiMethod(action, delegateService); } else if (action.ActionMethod == ActionMethodEnum.SQL) { ExecuteSQLMethod(action, delegateService); } else if (action.ActionMethod == ActionMethodEnum.StoreProcedure) { ExecuteStoreProcedureMethod(action, delegateService); } else if (action.ActionMethod == ActionMethodEnum.Python) { ExecutePythonMethod(action, delegateService); } else { throw new WorkflowException(string.Format("暂不支持的选项:{0}", action.ActionMethod.ToString())); } } }
/// <summary> /// 执行外部方法 /// SetVariable: /// https://stackoverflow.com/questions/26426955/setting-and-getting-variables-in-net-hosted-ironpython-script/45734097 /// </summary> /// <param name="action">Action实体</param> /// <param name="delegateService">委托服务</param> private static void ExecutePythonMethod(ActionEntity action, IDelegateService delegateService) { try { if (action.CodeInfo != null && !string.IsNullOrEmpty(action.CodeInfo.CodeText)) { // var pythonScript = action.Expression; var pythonScript = action.CodeInfo.CodeText; //modified by Besley in 12/26/2019, body is nodetext rather than attribute var engine = Python.CreateEngine(); var scope = engine.CreateScope(); var dictionary = CompositeKeyValue(action.Arguments, delegateService); foreach (var item in dictionary) { scope.SetVariable(item.Key, item.Value); } var source = engine.CreateScriptSourceFromString(pythonScript); source.Execute(scope); } else { throw new WorkflowException("Python脚本为空, 不能执行!"); } } catch (System.Exception ex) { throw new WorkflowException(string.Format("执行Pythond方法出错:{0}", ex.Message)); } }
/// <summary> /// 构造最终对象的Json字符串 /// </summary> /// <param name="arguments">参数列表</param> /// <param name="delegateService">委托服务</param> /// <returns>Json字符串</returns> private static string CompositeJsonValue(string arguments, IDelegateService delegateService) { var jsonValue = string.Empty; var arguValue = string.Empty; var arguList = arguments.Split(','); var strBuilder = new StringBuilder(256); foreach (var name in arguList) { if (strBuilder.ToString() != string.Empty) { strBuilder.Append(","); } arguValue = delegateService.GetVariableThroughly(name); arguValue = FormatJsonStringIfSimple(arguValue); strBuilder.Append(string.Format("{0}:{1}", name, arguValue)); } if (strBuilder.ToString() != string.Empty) { jsonValue = string.Format("{{{0}}}", strBuilder.ToString()); } return(jsonValue); }
/// <summary> /// 执行外部服务实现类 /// </summary> /// <param name="action">操作</param> /// <param name="delegateService">委托服务类</param> private static void Execute(ActionEntity action, IDelegateService delegateService) { if (action.ActionType == ActionTypeEnum.Event) { if (action.ActionMethod == ActionMethodEnum.LocalService) { ExecuteLocalService(action, delegateService); } else if (action.ActionMethod == ActionMethodEnum.CSharpLibrary) { ExecuteCSharpLibraryMethod(action, delegateService); } else if (action.ActionMethod == ActionMethodEnum.WebApi) { ExecuteWebApiMethod(action, delegateService); } else if (action.ActionMethod == ActionMethodEnum.SQL) { ExecuteSQLMethod(action, delegateService); } else if (action.ActionMethod == ActionMethodEnum.StoreProcedure) { ExecuteStoreProcedureMethod(action, delegateService); } else if (action.ActionMethod == ActionMethodEnum.Python) { ExecutePythonMethod(action, delegateService); } else { throw new WorkflowException(LocalizeHelper.GetEngineMessage("actionexecutor.Execute.exception", action.ActionMethod.ToString())); } } }
/// <summary> /// 执行外部操作的方法 /// </summary> /// <param name="actionList">操作列表</param> /// <param name="delegateService">委托方法</param> protected void ExecteActionList(IList <ActionEntity> actionList, IDelegateService delegateService) { if (actionList != null && actionList.Count > 0) { ActionExecutor.ExecteActionList(actionList, delegateService); } }
/// <summary> /// 构造可变数值列表 /// </summary> /// <param name="arguments">参数列表</param> /// <param name="delegateService">委托服务</param> /// <returns>参数列表</returns> private static object[] CompositeParameterValues(string arguments, IDelegateService delegateService) { var arguList = arguments.Split(','); object[] valueArray = new object[arguList.Length]; for (var i = 0; i < arguList.Length; i++) { valueArray[i] = delegateService.GetVariableThroughly(arguList[i]); } return(valueArray); }
/// <summary> /// 执行CSHARP方法 /// </summary> /// <param name="action">Action实体</param> /// <param name="delegateService">委托服务</param> private static void ExecuteCSharpCodeMethod(ActionEntity action, IDelegateService delegateService) { try { ; } catch (System.Exception ex) { throw new WorkflowException(string.Format("执行CSharp脚本出错:{0}", ex.Message)); } }
/// <summary> /// 触发后执行外部操作的方法 /// </summary> /// <param name="actionList">操作列表</param> /// <param name="delegateService">委托服务</param> internal static void ExecteActionListAfter(IList <ActionEntity> actionList, IDelegateService delegateService) { if (actionList != null && actionList.Count > 0) { var list = actionList.Where(a => a.FireType == FireTypeEnum.After).ToList(); if (list != null && list.Count > 0) { ActionExecutor.ExecteActionList(list, delegateService); } } }
/// <summary> /// Action 的执行方法 /// </summary> /// <param name="serviceList">操作列表</param> /// <param name="delegateService">参数列表</param> internal static void ExecteServiceList(IList <ServiceEntity> serviceList, IDelegateService delegateService) { if (serviceList != null && serviceList.Count > 0) { foreach (var service in serviceList) { if (service.Method != ServiceMethodEnum.None) { Execute(service, delegateService); } } } }
/// <summary> /// 执行外部服务实现类 /// </summary> /// <param name="action">操作</param> /// <param name="delegateService">委托服务类</param> private static void Execute(ActionEntity action, IDelegateService delegateService) { if (action.ActionType == ActionTypeEnum.Event) { if (action.ActionMethod == ActionMethodEnum.LocalMethod) { ExecuteLocalMethod(action, delegateService); } else { throw new WorkflowException(string.Format("社区版暂不支持的选项:{0}", action.ActionMethod.ToString())); } } }
/// <summary> /// 执行外部方法 /// </summary> /// <param name="action">Action实体</param> /// <param name="delegateService">委托服务</param> private static void ExecuteStoreProcedureMethod(ActionEntity action, IDelegateService delegateService) { try { var parameters = CompositeSqlParametersValue(action.Arguments, delegateService); var procedureName = action.Expression; var session = delegateService.GetSession(); var repository = new Repository(); repository.ExecuteProc(session.Connection, procedureName, parameters, session.Transaction); } catch (System.Exception ex) { throw new WorkflowException(string.Format("执行StoreProcedure方法出错:{0}", ex.Message)); } }
/// <summary> /// 执行外部方法 /// </summary> /// <param name="service">Action实体</param> /// <param name="delegateService">委托服务</param> private static void ExecuteStoreProcedureMethod(ServiceEntity service, IDelegateService delegateService) { try { var parameters = CompositeSqlParametersValue(service.Arguments, delegateService); var procedureName = service.Expression; var session = delegateService.GetSession(); var repository = new Repository(); repository.ExecuteProc(session.Connection, procedureName, parameters, session.Transaction); } catch (System.Exception ex) { throw new WorkflowException(LocalizeHelper.GetEngineMessage("actionexecutor.ExecuteStoreProcedureMethod.exception", ex.Message)); } }
/// <summary> /// 执行外部方法 /// </summary> /// <param name="service">Action实体</param> /// <param name="delegateService">委托服务</param> private static void ExecuteLocalService(ServiceEntity service, IDelegateService delegateService) { try { //先获取具体实现类 var instance = ReflectionHelper.GetSpecialInstance <IExternalService>(service.Expression); //再调用基类可执行方法 var exterableInstance = instance as IExternable; exterableInstance.Executable(delegateService); } catch (System.Exception ex) { throw new WorkflowException(LocalizeHelper.GetEngineMessage("actionexecutor.ExecuteLocalService.exception", ex.Message)); } }
/// <summary> /// Action 的执行方法 /// </summary> /// <param name="actionList">操作列表</param> /// <param name="delegateService">参数列表</param> internal static void ExecteActionList(IList <ActionEntity> actionList, IDelegateService delegateService) { if (actionList != null && actionList.Count > 0) { foreach (var action in actionList) { if (action.FireType != FireTypeEnum.None && (action.ActionMethod != ActionMethodEnum.None)) { Execute(action, delegateService); } } } }
/// <summary> /// Action 的执行方法 /// </summary> /// <param name="actionList">操作列表</param> /// <param name="delegateService">参数列表</param> internal static void ExecteActionList(IList <ActionEntity> actionList, IDelegateService delegateService) { if (actionList != null && actionList.Count > 0) { foreach (var action in actionList) { if (action.FireType != FireTypeEnum.None && !string.IsNullOrEmpty(action.Expression)) { Execute(action, delegateService); } } } }
/// <summary> /// 执行外部方法 /// </summary> /// <param name="action">Action实体</param> /// <param name="delegateService">委托服务</param> private static void ExecuteLocalMethod(ActionEntity action, IDelegateService delegateService) { try { //先获取具体实现类 var instance = ReflectionHelper.GetSpecialInstance <IExternalService>(action.Expression); //再调用基类可执行方法 var exterableInstance = instance as IExternable; exterableInstance.Executable(delegateService); } catch (System.Exception ex) { throw new WorkflowException(string.Format("执行LocalMethod出错:{0}", ex.Message)); } }
/// <summary> /// 执行外部方法 /// </summary> /// <param name="service">Action实体</param> /// <param name="delegateService">委托服务</param> private static void ExecuteWebApiMethod(ServiceEntity service, IDelegateService delegateService) { try { object result = null; if (service.SubMethod == SubMethodEnum.HttpGet) { var jsonGetValue = delegateService.GetVariableThroughly(service.Arguments); var url = string.Format("{0}/{1}", service.Expression, jsonGetValue); var httpGetClient = HttpClientHelper.CreateHelper(url); result = httpGetClient.Get(); } else if (service.SubMethod == SubMethodEnum.HttpPost) { string url = service.Expression; var httpClientHelper = HttpClientHelper.CreateHelper(url); var jsonValue = CompositeJsonValue(service.Arguments, delegateService); var httpPostClient = HttpClientHelper.CreateHelper(url); result = httpClientHelper.Post(jsonValue); } else if (service.SubMethod == SubMethodEnum.HttpPut) { string url = service.Expression; var httpClientHelper = HttpClientHelper.CreateHelper(url); var jsonValue = CompositeJsonValue(service.Arguments, delegateService); var httpPostClient = HttpClientHelper.CreateHelper(url); result = httpClientHelper.Put(jsonValue); } else if (service.SubMethod == SubMethodEnum.HttpDelete) { var jsonGetValue = delegateService.GetVariableThroughly(service.Arguments); var url = string.Format("{0}/{1}", service.Expression, jsonGetValue); var httpGetClient = HttpClientHelper.CreateHelper(url); result = httpGetClient.Delete(); } } catch (System.Exception ex) { throw new WorkflowException(LocalizeHelper.GetEngineMessage("actionexecutor.ExecuteWebApi.exception", ex.Message)); } }
/// <summary> /// 执行外部方法 /// </summary> /// <param name="action">Action实体</param> /// <param name="delegateService">委托服务</param> private static void ExecuteSQLMethod(ActionEntity action, IDelegateService delegateService) { try { var parameters = CompositeSqlParametersValue(action.Arguments, delegateService); if (action.CodeInfo != null && !string.IsNullOrEmpty(action.CodeInfo.CodeText)) { //var sqlScript = action.Expression; var sqlScript = action.CodeInfo.CodeText; //modified by Besley in 12/26/2019, body is nodetext rather than attribute var session = delegateService.GetSession(); var repository = new Repository(); repository.Execute(session.Connection, sqlScript, parameters, session.Transaction); } else { throw new WorkflowException("SQL脚本为空, 不能执行!"); } } catch (System.Exception ex) { throw new WorkflowException(string.Format("执行SQL脚本时出错:{0}", ex.Message)); } }
/// <summary> /// 执行外部方法 /// </summary> /// <param name="service">Action实体</param> /// <param name="delegateService">委托服务</param> private static void ExecuteSQLMethod(ServiceEntity service, IDelegateService delegateService) { try { var parameters = CompositeSqlParametersValue(service.Arguments, delegateService); if (service.CodeInfo != null && !string.IsNullOrEmpty(service.CodeInfo.CodeText)) { //var sqlScript = action.Expression; var sqlScript = service.CodeInfo.CodeText; //modified by Besley in 12/26/2019, body is nodetext rather than attribute var session = delegateService.GetSession(); var repository = new Repository(); repository.Execute(session.Connection, sqlScript, parameters, session.Transaction); } else { throw new WorkflowException(LocalizeHelper.GetEngineMessage("actionexecutor.ExecuteSQLMethod.warn")); } } catch (System.Exception ex) { throw new WorkflowException(LocalizeHelper.GetEngineMessage("actionexecutor.ExecuteSQLMethod.exception", ex.Message)); } }
/// <summary> /// 设置委托服务 /// </summary> /// <param name="delegateService">委托服务</param> public void Executable(IDelegateService delegateService) { DelegateService = delegateService; Execute(); }
/// <summary> /// 构造最终对象的Json字符串 /// </summary> /// <param name="arguments">参数列表</param> /// <param name="delegateService">委托服务</param> /// <returns>字典列表</returns> private static IDictionary <string, string> CompositeKeyValue(string arguments, IDelegateService delegateService) { var dictionary = new Dictionary <string, string>(); var arguValue = string.Empty; var arguList = arguments.Split(','); foreach (var name in arguList) { arguValue = delegateService.GetVariableThroughly(name); dictionary.Add(name, arguValue); } return(dictionary); }
/// <summary> /// 构造最终对象的Json字符串 /// </summary> /// <param name="arguments">参数列表</param> /// <param name="delegateService">委托服务</param> /// <returns>动态参数列表</returns> private static DynamicParameters CompositeSqlParametersValue(string arguments, IDelegateService delegateService) { DynamicParameters parameters = new DynamicParameters(); var arguValue = string.Empty; var arguList = arguments.Split(','); foreach (var name in arguList) { arguValue = delegateService.GetVariableThroughly(name); parameters.Add(string.Format("@{0}", name), arguValue); } return(parameters); }
public DelegateServiceTests() { _marketplaceRepository = new Mock <IDelegateRepository>(); _jwtIdClaimReaderHelper = new Mock <IJwtIdClaimReaderHelper>(); _marketplaceService = new DelegateService(_marketplaceRepository.Object, _jwtIdClaimReaderHelper.Object); }
public DelegateController(IDelegateService delegateService) { _delegateService = delegateService; }