public override void Execute(IExecutionEntity execution) { execution.CurrentFlowElement.ExtensionElements.TryGetValue(BpmnXMLConstants.ELEMENT_EXTENSIONS_PROPERTY, out IList <ExtensionElement> pElements); if (pElements != null) { try { WebApiParameter parameter = new WebApiParameter(execution, pElements); string url = parameter.Url; string dataObj = parameter.VariableName; string method = parameter.Method; var httpProxy = ProcessEngineServiceProvider.Resolve <IServiceWebApiHttpProxy>(); HttpContext httpContext = ProcessEngineServiceProvider.Resolve <IHttpContextAccessor>()?.HttpContext; if (httpContext == null) { IAccessTokenProvider accessTokenProvider = ProcessEngineServiceProvider.Resolve <IAccessTokenProvider>(); accessTokenProvider.SetHttpClientRequestAccessToken(httpProxy.HttpClient, null, execution.TenantId); } switch (method?.ToLower()) { default: case "get": ExecuteGet(execution, url, parameter.Request, dataObj, httpProxy); break; case "post": ExecutePost(execution, url, parameter.Request, dataObj, httpProxy); break; } Leave(execution); } catch (Exception ex) { throw new BpmnError(Context.CommandContext.ProcessEngineConfiguration.WebApiErrorCode, ex.Message); } } }
private void ExecuteDebugMode(IExecutionEntity execution) { execution.CurrentFlowElement.ExtensionElements.TryGetValue(BpmnXMLConstants.ELEMENT_EXTENSIONS_PROPERTY, out IList <ExtensionElement> pElements); if (pElements != null) { var parameter = new WebApiParameter(execution, pElements); string dataObj = parameter.VariableName; if (!string.IsNullOrEmpty(dataObj)) { try { execution.SetVariable(dataObj, parameter.MockData); } catch (Exception ex) { logger.LogError($"调用服务失败MockData=${dataObj}\r\n${ex.Message}${ex.StackTrace}"); string errorCode = execution.CurrentFlowElement.GetExtensionElementAttributeValue("errorCode"); BpmnError error; if (string.IsNullOrWhiteSpace(errorCode) == false) { error = new BpmnError(errorCode, ex.Message); } else { error = new BpmnError(Context.CommandContext.ProcessEngineConfiguration.WebApiErrorCode, ex.Message); } ErrorPropagation.PropagateError(error, execution); } } } }
public override void Execute(IExecutionEntity execution) { execution.CurrentFlowElement.ExtensionElements.TryGetValue(BpmnXMLConstants.ELEMENT_EXTENSIONS_PROPERTY, out IList <ExtensionElement> pElements); if (pElements != null) { Exception exception = null; Stopwatch sw = new Stopwatch(); string url = null; string dataObj = null; object request = null; try { sw.Start(); WebApiParameter parameter = new WebApiParameter(execution, pElements); url = parameter.Url; dataObj = parameter.VariableName; request = parameter.Request; string method = parameter.Method; var httpProxy = ProcessEngineServiceProvider.Resolve <IHttpClientProxy>(); HttpContext httpContext = ProcessEngineServiceProvider.Resolve <IHttpContextAccessor>()?.HttpContext; if (httpContext == null) { httpProxy.SetHttpClientRequestAccessToken(execution.StartUserId, execution.TenantId); //IAccessTokenProvider accessTokenProvider = ProcessEngineServiceProvider.Resolve<IAccessTokenProvider>(); //accessTokenProvider.SetHttpClientRequestAccessToken(httpProxy.HttpClient, execution.StartUserId, execution.TenantId); } switch (method?.ToLower()) { default: case "get": ExecuteGet(execution, url, request, dataObj, httpProxy); break; case "post": ExecutePost(execution, url, request, dataObj, httpProxy); break; } sw.Stop(); logger.LogInformation($"调用外部服务共计({sw.ElapsedMilliseconds}ms) url={url} request={(request is null ? "" : JsonConvert.SerializeObject(request))}"); Leave(execution); } catch (Exception ex) { sw.Stop(); logger.LogError($"调用外部服务失败({sw.ElapsedMilliseconds}ms) url={url} request={(request is null ? "" : JsonConvert.SerializeObject(request))}:\r\n" + ex.Message + ex.StackTrace); throw new BpmnError(Context.CommandContext.ProcessEngineConfiguration.WebApiErrorCode, ex.Message); } } }
/// <summary> /// /// </summary> /// <param name="execution"></param> public override void Execute(IExecutionEntity execution) { execution.CurrentFlowElement.ExtensionElements.TryGetValue(BpmnXMLConstants.ELEMENT_EXTENSIONS_PROPERTY, out IList <ExtensionElement> pElements); _ = bool.TryParse(execution.GetVariableInstance(DynamicBpmnConstants.PORCESS_DEBUGMODE_VARIABLE)?.Value?.ToString(), out var debugMode); WebApiParameter parameter = new WebApiParameter(execution, pElements); if (parameter.IsMock.HasValue ? parameter.IsMock.Value : debugMode) { ExecuteDebugMode(execution); } else { if (pElements != null) { Stopwatch sw = new Stopwatch(); string url = null; object request = null; try { sw.Start(); url = parameter.Url; string dataObj = parameter.VariableName; request = parameter.Request; string method = parameter.Method; var httpProxy = ProcessEngineServiceProvider.Resolve <IHttpClientProxy>(); HttpContext httpContext = ProcessEngineServiceProvider.Resolve <IHttpContextAccessor>()?.HttpContext; if (httpContext == null) { var uid = string.IsNullOrWhiteSpace(execution.StartUserId) ? Guid.NewGuid().ToString() : execution.StartUserId; httpProxy.SetHttpClientRequestAccessToken(uid, execution.TenantId, isSessionHeader: false); } switch (method?.ToLower()) { default: case "get": ExecuteGet(execution, url, request, dataObj, httpProxy); break; case "post": ExecutePost(execution, url, request, dataObj, httpProxy); break; } sw.Stop(); logger.LogInformation($"调用外部服务共计({sw.ElapsedMilliseconds}ms) url={url} request={(request is null ? "" : JsonConvert.SerializeObject(request))}"); } catch (Exception ex) { sw.Stop(); logger.LogError($"调用外部服务失败({sw.ElapsedMilliseconds}ms) url={url} request={(request is null ? "" : JsonConvert.SerializeObject(request))}:\r\n" + ex.Message + ex.StackTrace); string errorCode = execution.CurrentFlowElement.GetExtensionElementAttributeValue("errorCode"); BpmnError error; if (string.IsNullOrWhiteSpace(errorCode) == false) { error = new BpmnError(errorCode, ex.Message); } else { error = new BpmnError(Context.CommandContext.ProcessEngineConfiguration.WebApiErrorCode, ex.Message); } ErrorPropagation.PropagateError(error, execution); } } } Leave(execution); }