コード例 #1
0
        public async Task <Exception> Do(HttpResponseMessage response)
        {
            var requestBody = await response.RequestMessage.Content.ReadAsStringAsync();

            var strContent = await response.Content.ReadAsStringAsync();

            var error           = JsonSerializerHelper.Deserialize <CrmWebApiError>(strContent);
            UtilityException ex = null;
            TextFragment     fragment;

            switch ((int)response.StatusCode)
            {
            case 412:
                fragment = new TextFragment()
                {
                    Code = TextCodes.CrmWebApiConcurrencyError,
                    DefaultFormatting = "调用Crm的webapi出现并发性错误,Uri:{0},Body:{1},错误信息:{2}",
                    ReplaceParameters = new List <object>()
                    {
                        response.RequestMessage.RequestUri.ToString(), requestBody, strContent
                    }
                };


                ex = new UtilityException((int)Errors.CrmWebApiConcurrencyError, fragment);
                break;

            case 429:
                fragment = new TextFragment()
                {
                    Code = TextCodes.CrmWebApiLimitError,
                    DefaultFormatting = "调用Crm的webapi出现限制性错误,Uri:{0},Body:{1},错误信息:{2}",
                    ReplaceParameters = new List <object>()
                    {
                        response.RequestMessage.RequestUri.ToString(), requestBody, strContent
                    }
                };

                ex = new UtilityException((int)Errors.CrmWebApiLimitError, fragment);
                ex.Data[CrmWebApiResponseKeyNames.RetryAfter] = response.Headers.RetryAfter.Delta.Value;
                break;

            default:
                fragment = new TextFragment()
                {
                    Code = TextCodes.CrmWebApiCommonError,
                    DefaultFormatting = "调用Crm的webapi出现错误,Uri:{0},Body:{1},错误信息:{2}",
                    ReplaceParameters = new List <object>()
                    {
                        response.RequestMessage.RequestUri.ToString(), requestBody, strContent
                    }
                };
                ex = new UtilityException((int)Errors.CrmWebApiCommonError, fragment);
                break;
            }

            return(ex);
        }
コード例 #2
0
        public void Constructor_1_OK()
        {
            // Arrange:

            // Act:
            var ex1 = new UtilityException("test");

            // Assert:
        }
コード例 #3
0
        public void Constructor_0_OK()
        {
            // Arrange:

            // Act:
            var ex0 = new UtilityException();

            // Assert:
        }
コード例 #4
0
        public void Constructor_2_OK()
        {
            // Arrange:

            // Act:
            var ex2 =
                new UtilityException(
                    "test",
                    new Exception());

            // Assert:
        }
コード例 #5
0
 /// <summary>
 /// 通过指定的服务接口类型,初始化RegisterServiceAttribute新实例
 /// </summary>
 /// <param name="serviceType">指定的服务接口类型</param>
 public ServiceAttribute(Type serviceType)
 {
     if (serviceType == null)
     {
         throw UtilityException.ArgumentNull("serviceType");
     }
     if (serviceType.IsInterface == false)
     {
         throw UtilityException.Argument("serviceType", Resources.ServiceTypeIsNotInterface, serviceType.FullName);
     }
     this.ServiceType = serviceType;
 }
コード例 #6
0
        public async Task <object> Execute(RealWorkfolwActivityParameter parameter, RealWorkfolwContext context)
        {
            if (!_parameterHandleFactories.TryGetValue(parameter.ExpressionType, out IFactory <IRealWorkfolwActivityParameterHandle> handleFactory))
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.NotFoundRealWorkfolwActivityParameterHandleByType,
                    DefaultFormatting = "找不到表达式类型为{0}的工作流活动参数处理",
                    ReplaceParameters = new List <object>()
                    {
                        parameter.ExpressionType
                    }
                };

                throw new UtilityException((int)Errors.NotFoundRealWorkfolwActivityParameterHandleByType, fragment);
            }
            var value = await handleFactory.Create().Execute(parameter, context);

            var dataHandle = _realWorkfolwActivityParameterDataHandleSelector.Choose(parameter.DataType);
            var result     = await dataHandle.Convert(value);

            if (!await dataHandle.ValidateType(result))
            {
                string strResult;
                if (result == null)
                {
                    strResult = "null";
                }
                else
                {
                    strResult = result.GetType().FullName;
                }

                var fragment = new TextFragment()
                {
                    Code = TextCodes.RealWorkfolwActivityParameterDataHandleValidateError,
                    DefaultFormatting = "工作流活动参数数据处理验证失败,验证器数据类型为{0},要验证的实际数据类型为{1},参数名称为{2}",
                    ReplaceParameters = new List <object>()
                    {
                        parameter.DataType, strResult, parameter.Name
                    }
                };

                var exception = new UtilityException((int)Errors.RealWorkfolwActivityParameterDataHandleValidateError, fragment);
                exception.Data[UtilityExceptionDataKeys.Catch] = true;
                throw exception;
            }
            return(result);
        }
コード例 #7
0
ファイル: QueryMetadata.cs プロジェクト: gyb333/KDS3
        public static string GetRightExpression(EDataBaseType dataType, EValueType valueType, string value, List <string> parameters, bool isarray)
        {
            switch (valueType)
            {
            case EValueType.Const:
                return(DataTypeMetadata.GenerateValueExpression(dataType, value, false));

            case EValueType.Parameter:
                parameters.Add(value);
                return(value);

            case EValueType.Property:
                return("item." + value);
            }
            throw UtilityException.NotSupported("不支持查询值类型");
        }
コード例 #8
0
ファイル: QueryMetadata.cs プロジェクト: gyb333/KDS3
        public static EDataBaseType GetLeftDataType(EntityMetadata entity, string path)
        {
            string[] names = path.Split(',');

            if (names.Length > 1)
            {
                if (entity.NavigationPropertys.ContainsKey(names[0]))
                {
                    return(GetLeftDataType(entity.NavigationPropertys[names[0]].Entity, names[1]));
                }
            }
            else
            {
                var member = entity.Definition.Members.Where(a => a.Name == names[0]).FirstOrDefault();
                if (member != null)
                {
                    var content = member.Content as CommonDataType;
                    return(content.BaseType);
                }
            }
            throw UtilityException.NotSupported("查询表达式数据类型检索失败");
        }
コード例 #9
0
        private async Task <List <RealWorkfolwActivityDescriptionDataForConditionElseIf> > GetElseifs(string activityConfiguration, XElement configuration)
        {
            List <RealWorkfolwActivityDescriptionDataForConditionElseIf> elseifList = new List <RealWorkfolwActivityDescriptionDataForConditionElseIf>();
            var elseifElements = configuration.Elements("elseif");

            if (elseifElements != null && elseifElements.Count() > 0)
            {
                foreach (var elseifElementItem in elseifElements)
                {
                    var parametersElement = elseifElementItem.Element("parameters");
                    if (parametersElement == null)
                    {
                        var fragment = new TextFragment()
                        {
                            Code = TextCodes.RealWorkfolwActivityConfigurationParseXMLError,
                            DefaultFormatting = "工作流活动配置转换成XML时出错,活动配置:{0},错误原因:{1}",
                            ReplaceParameters = new List <object>()
                            {
                                activityConfiguration, new TextFragment()
                                {
                                    Code = TextCodes.RealWorkfolwActivityConfigurationMissXMLChildElement, DefaultFormatting = "工作流活动配置缺少XML节点{0},节点所属的父节点内容:{1}", ReplaceParameters = new List <object>()
                                    {
                                        "parameters", elseifElementItem.ToString()
                                    }
                                }
                            }
                        };

                        var exception = new UtilityException((int)Errors.RealWorkfolwActivityConfigurationParseXMLError, fragment);
                        exception.Data[UtilityExceptionDataKeys.Catch] = true;
                        throw exception;
                    }
                    var conditionParameterElement = (from parameter in parametersElement.Elements("parameter")
                                                     where parameter.Attribute("name").Value == "condition"
                                                     select parameter).FirstOrDefault();
                    if (conditionParameterElement == null)
                    {
                        var fragment = new TextFragment()
                        {
                            Code = TextCodes.RealWorkfolwActivityConfigurationParseXMLError,
                            DefaultFormatting = "工作流活动配置转换成XML时出错,活动配置:{0},错误原因:{1}",
                            ReplaceParameters = new List <object>()
                            {
                                activityConfiguration, new TextFragment()
                                {
                                    Code = TextCodes.RealWorkfolwActivityConfigurationMissXMLChildElement, DefaultFormatting = "工作流活动配置缺少XML节点{0},节点所属的父节点内容:{1}", ReplaceParameters = new List <object>()
                                    {
                                        "parameters/parameter[name=condition]", elseifElementItem.ToString()
                                    }
                                }
                            }
                        };

                        var exception = new UtilityException((int)Errors.RealWorkfolwActivityConfigurationParseXMLError, fragment);
                        exception.Data[UtilityExceptionDataKeys.Catch] = true;
                        throw exception;
                    }

                    var conditionParameter = await _parameterConfigurationService.ResolveParameter(conditionParameterElement.ToString());

                    var matchActivities = await _activityConfigurationService.SeparateActivities(elseifElementItem.ToString());

                    List <RealWorkfolwActivityDescription> matchList = new List <RealWorkfolwActivityDescription>();

                    foreach (var itemMatchActivity in matchActivities)
                    {
                        var match = await _realWorkfolwActivityResolve.Execute(itemMatchActivity.Configuration);

                        matchList.Add(match);
                    }

                    elseifList.Add(new RealWorkfolwActivityDescriptionDataForConditionElseIf()
                    {
                        Condition = conditionParameter,
                        Match     = matchList
                    });
                }
            }
            return(elseifList);
        }
コード例 #10
0
        public async Task <RealWorkfolwActivityDescription> Execute(string activityConfiguration)
        {
            XElement configuration = null;

            try
            {
                configuration = XElement.Parse(activityConfiguration);
            }
            catch (Exception ex)
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.RealWorkfolwActivityConfigurationParseXMLError,
                    DefaultFormatting = "工作流活动配置转换成XML时出错,活动配置:{0},错误原因:{1}",
                    ReplaceParameters = new List <object>()
                    {
                        activityConfiguration, ex.Message
                    }
                };
                var exception = new UtilityException((int)Errors.RealWorkfolwActivityConfigurationParseXMLError, fragment);
                exception.Data[UtilityExceptionDataKeys.Catch] = true;
                throw exception;
            }

            RealWorkfolwActivityDescriptionDataForWhile data = new RealWorkfolwActivityDescriptionDataForWhile();


            var activitiesElement = configuration.Element("activities");

            if (activitiesElement == null)
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.RealWorkfolwActivityConfigurationParseXMLError,
                    DefaultFormatting = "工作流活动配置转换成XML时出错,活动配置:{0},错误原因:{1}",
                    ReplaceParameters = new List <object>()
                    {
                        activityConfiguration, new TextFragment()
                        {
                            Code = TextCodes.RealWorkfolwActivityConfigurationMissXMLElement, DefaultFormatting = "工作流活动配置缺少XML节点{0}", ReplaceParameters = new List <object>()
                            {
                                "activities"
                            }
                        }
                    }
                };

                var exception = new UtilityException((int)Errors.RealWorkfolwActivityConfigurationParseXMLError, fragment);
                exception.Data[UtilityExceptionDataKeys.Catch] = true;
                throw exception;
            }

            var activities = await _activityConfigurationService.SeparateActivities(activitiesElement.ToString());

            List <RealWorkfolwActivityDescription> activityDescriptionList = new List <RealWorkfolwActivityDescription>();

            foreach (var itemActivity in activities)
            {
                var activityDescription = await _realWorkfolwActivityResolve.Execute(itemActivity.Configuration);

                activityDescriptionList.Add(activityDescription);
            }

            RealWorkfolwActivityDescription description = new RealWorkfolwActivityDescription()
            {
                Data = data
            };

            return(description);
        }
コード例 #11
0
        public async Task <RealWorkfolwActivityDescription> Execute(string activityConfiguration)
        {
            XElement configuration = null;

            try
            {
                configuration = XElement.Parse(activityConfiguration);
            }
            catch (Exception ex)
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.RealWorkfolwActivityConfigurationParseXMLError,
                    DefaultFormatting = "工作流活动配置转换成XML时出错,活动配置:{0},错误原因:{1}",
                    ReplaceParameters = new List <object>()
                    {
                        activityConfiguration, ex.Message
                    }
                };

                var exception = new UtilityException((int)Errors.RealWorkfolwActivityConfigurationParseXMLError, fragment);
                exception.Data[UtilityExceptionDataKeys.Catch] = true;
                throw exception;
            }

            RealWorkfolwActivityDescriptionDataForTransaction data = new RealWorkfolwActivityDescriptionDataForTransaction();

            data.Activities = new List <RealWorkfolwActivityDescription>();

            var scopeAttribute = configuration.Attribute("scope");

            if (scopeAttribute == null)
            {
                data.Scope = "1";
            }
            else
            {
                data.Scope = scopeAttribute.Value;
            }

            var levelAttribute = configuration.Attribute("level");

            if (levelAttribute == null)
            {
                data.Level = "1";
            }
            else
            {
                data.Level = levelAttribute.Value;
            }

            var timeoutAttribute = configuration.Attribute("timeout");

            if (timeoutAttribute != null)
            {
                try
                {
                    data.Timeout = TimeSpan.Parse(timeoutAttribute.Value);
                }
                catch
                {
                    var fragment = new TextFragment()
                    {
                        Code = TextCodes.RealWorkfolwActivityConfigurationAttributeParseTypeError,
                        DefaultFormatting = "工作流活动配置中的属性{0}的值无法转换为类型{1},属性值为{2},所属节点的值为{3}",
                        ReplaceParameters = new List <object>()
                        {
                            "timeout", typeof(TimeSpan).FullName, timeoutAttribute.Value, configuration.ToString()
                        }
                    };

                    throw new UtilityException((int)Errors.RealWorkfolwActivityConfigurationAttributeParseTypeError, fragment);
                }
            }



            var activitiesElement = configuration.Element("activities");

            if (activitiesElement != null)
            {
                var activitise = await _activityConfigurationService.SeparateActivities(activitiesElement.ToString());

                foreach (var activityItem in activitise)
                {
                    var activityDescription = await _realWorkfolwActivityResolve.Execute(activityItem.Configuration);

                    data.Activities.Add(activityDescription);
                }
            }

            RealWorkfolwActivityDescription description = new RealWorkfolwActivityDescription()
            {
                Data = data
            };

            return(description);
        }
コード例 #12
0
        public async Task <Dictionary <string, object> > Execute(RealWorkfolwActivityDescription activityDescription, RealWorkfolwContext context, Dictionary <string, object> runtimeParameters)
        {
            Exception exception;
            RealWorkfolwActivityDescriptionDataForWhile data = activityDescription.Data as RealWorkfolwActivityDescriptionDataForWhile;

            if (data == null)
            {
                string realType;
                if (activityDescription.Data == null)
                {
                    realType = "null";
                }
                else
                {
                    realType = activityDescription.Data.GetType().FullName;
                }

                var fragment = new TextFragment()
                {
                    Code = TextCodes.RealWorkfolwActivityDescriptionDataTypeNotMatch,
                    DefaultFormatting = "工作流活动描述的Data属性的类型不匹配,期待的类型为{0},实际类型为{1},发生位置:{2}",
                    ReplaceParameters = new List <object>()
                    {
                        typeof(RealWorkfolwActivityDescriptionDataForWhile).FullName, realType, $"{this.GetType().FullName}.Execute"
                    }
                };

                throw new UtilityException((int)Errors.RealWorkfolwActivityDescriptionDataTypeNotMatch, fragment);
            }

            if (!activityDescription.InputParameters.TryGetValue("condition", out RealWorkfolwActivityParameter conditionParameter))
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.NotFoundRealWorkfolwActivityDescriptionInputByName,
                    DefaultFormatting = "名称为{0}的工作流描述中找不到名称为{1}的输入参数",
                    ReplaceParameters = new List <object>()
                    {
                        activityDescription.Name, "condition"
                    }
                };

                exception = new UtilityException((int)Errors.NotFoundRealWorkfolwActivityDescriptionInputByName, fragment);
                exception.Data[UtilityExceptionDataKeys.Catch] = true;
                throw exception;
            }

            while (true)
            {
                string   newName   = string.Empty;;
                string[] arrayName = conditionParameter.Name.Split('_');
                if (arrayName.Length > 1)
                {
                    arrayName[arrayName.Length - 1] = Guid.NewGuid().ToString();
                    foreach (var itemName in arrayName)
                    {
                        if (newName == string.Empty)
                        {
                            newName = itemName;
                        }
                        else
                        {
                            newName = $"{newName}_{itemName}";
                        }
                    }
                }
                conditionParameter.Name = newName;

                var conditionParameterResultObj = await _realWorkfolwActivityParameterHandle.Execute(conditionParameter, context);

                var conditionParameterResult = conditionParameterResultObj as bool?;
                if (conditionParameterResult == null)
                {
                    string realType;
                    if (conditionParameterResultObj == null)
                    {
                        realType = null;
                    }
                    else
                    {
                        realType = conditionParameterResultObj.GetType().FullName;
                    }

                    var fragment = new TextFragment()
                    {
                        Code = TextCodes.RealWorkfolwActivityDescriptionInputResultTypeNotMatch,
                        DefaultFormatting = "名称为{0}的工作流描述中名称为{1}的输入参数的结果值类型不匹配,期待的类型为{2},实际类型为{3},参数配置为{4}",
                        ReplaceParameters = new List <object>()
                        {
                            activityDescription.Name, "condition", typeof(bool).FullName, realType, conditionParameter.Configuration
                        }
                    };

                    exception = new UtilityException((int)Errors.RealWorkfolwActivityDescriptionInputResultTypeNotMatch, fragment);
                    exception.Data[UtilityExceptionDataKeys.Catch] = true;
                    throw exception;
                }

                if (conditionParameterResult.Value)
                {
                    await ExecuteMatch(data.Activities, context);
                }
                else
                {
                    break;
                }
            }

            Dictionary <string, object> outputDict = new Dictionary <string, object>();

            foreach (var outputItem in activityDescription.OutputParameters)
            {
                var outputResult = await _realWorkfolwActivityParameterHandle.Execute(outputItem.Value, context);

                outputDict[outputItem.Key] = outputResult;
            }

            return(outputDict);
        }
コード例 #13
0
        public async Task <RealWorkfolwActivityDescription> Execute(string activityConfiguration)
        {
            XElement configuration = null;

            try
            {
                configuration = XElement.Parse(activityConfiguration);
            }
            catch (Exception ex)
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.RealWorkfolwActivityConfigurationParseXMLError,
                    DefaultFormatting = "工作流活动配置转换成XML时出错,活动配置:{0},错误原因:{1}",
                    ReplaceParameters = new List <object>()
                    {
                        activityConfiguration, ex.Message
                    }
                };
                var exception = new UtilityException((int)Errors.RealWorkfolwActivityConfigurationParseXMLError, fragment);
                exception.Data[UtilityExceptionDataKeys.Catch] = true;
                throw exception;
            }

            RealWorkfolwActivityDescriptionDataForParallel data = new RealWorkfolwActivityDescriptionDataForParallel();

            data.Items = new List <RealWorkfolwActivityDescriptionDataForParallelItem>();

            var maxAttribute = configuration.Attribute("max");

            if (maxAttribute == null)
            {
                data.Max = 4;
            }
            else
            {
                if (!int.TryParse(maxAttribute.Value, out int max))
                {
                    var fragment = new TextFragment()
                    {
                        Code = TextCodes.RealWorkfolwActivityConfigurationAttributeParseTypeError,
                        DefaultFormatting = "工作流活动配置中的属性{0}的值无法转换为类型{1},属性值为{2},所属节点的值为{3}",
                        ReplaceParameters = new List <object>()
                        {
                            "max", typeof(int).FullName, maxAttribute.Value, configuration.ToString()
                        }
                    };

                    throw new UtilityException((int)Errors.RealWorkfolwActivityConfigurationAttributeParseTypeError, fragment);
                }
                data.Max = max;
            }

            var itemsElement = configuration.Element("items");

            if (itemsElement != null)
            {
                var itemElementList = itemsElement.Elements("item");
                foreach (var itemElement in itemElementList)
                {
                    var activitiesElement  = itemsElement.Element("activities");
                    var errorHandleElement = itemsElement.Element("errorhandle");

                    RealWorkfolwActivityDescriptionDataForParallelItem item = new RealWorkfolwActivityDescriptionDataForParallelItem()
                    {
                        Activities  = new List <RealWorkfolwActivityDescription>(),
                        ErrorHandle = null
                    };

                    data.Items.Add(item);

                    if (activitiesElement != null)
                    {
                        var activitise = await _activityConfigurationService.SeparateActivities(activitiesElement.ToString());

                        foreach (var activityItem in activitise)
                        {
                            var activityDescription = await _realWorkfolwActivityResolve.Execute(activityItem.Configuration);

                            item.Activities.Add(activityDescription);
                        }
                    }

                    if (errorHandleElement != null && errorHandleElement.FirstNode != null)
                    {
                        var errorHandleActivity = await _activityConfigurationService.ResolveActivity(errorHandleElement.FirstNode.ToString());

                        var errorHandleActivityDescription = await _realWorkfolwActivityResolve.Execute(errorHandleActivity.Configuration);

                        item.ErrorHandle = errorHandleActivityDescription;
                    }
                }
            }

            RealWorkfolwActivityDescription description = new RealWorkfolwActivityDescription()
            {
                Data = data
            };

            return(description);
        }
コード例 #14
0
ファイル: CrmService.cs プロジェクト: rhw1111/DCEM
        public async Task Execute(CrmRequestMessage request, Func <Task <HttpResponseMessage> > action)
        {
            int retryNumber = 0;

            while (true)
            {
                var response = await action();

                retryNumber++;
                if (!response.IsSuccessStatusCode)
                {
                    //先判断content为空
                    var requestBody = string.Empty;
                    if (response.RequestMessage.Content != null)
                    {
                        requestBody = await response.RequestMessage.Content.ReadAsStringAsync();
                    }
                    var strContent = await response.Content.ReadAsStringAsync();

                    //这个后面没用到,还容易报错,暂时注释
                    //var error = JsonSerializerHelper.Deserialize<CrmWebApiError>(strContent);
                    UtilityException ex          = null;
                    bool             canContinue = false;
                    TextFragment     fragment;
                    switch ((int)response.StatusCode)
                    {
                    case 412:
                        fragment = new TextFragment()
                        {
                            Code = TextCodes.CrmWebApiConcurrencyError,
                            DefaultFormatting = "调用Crm的webapi出现并发性错误,Uri:{0},Body:{1},错误信息:{2}",
                            ReplaceParameters = new List <object>()
                            {
                                response.RequestMessage.RequestUri.ToString(), requestBody, strContent
                            }
                        };
                        ex = new UtilityException((int)Errors.CrmWebApiConcurrencyError, fragment);
                        break;

                    case 429:
                        if (retryNumber >= request.MaxRetry)
                        {
                            fragment = new TextFragment()
                            {
                                Code = TextCodes.CrmWebApiLimitError,
                                DefaultFormatting = "调用Crm的webapi出现限制性错误,Uri:{0},Body:{1},错误信息:{2}",
                                ReplaceParameters = new List <object>()
                                {
                                    response.RequestMessage.RequestUri.ToString(), requestBody, strContent
                                }
                            };

                            ex = new UtilityException((int)Errors.CrmWebApiLimitError, fragment);
                        }
                        else
                        {
                            await Task.Delay(response.Headers.RetryAfter.Delta.Value);

                            //System.Threading.Thread.Sleep(response.Headers.RetryAfter.Delta.Value);
                            canContinue = true;
                        }
                        break;

                    default:
                        fragment = new TextFragment()
                        {
                            Code = TextCodes.CrmWebApiCommonError,
                            DefaultFormatting = "调用Crm的webapi出现错误,Uri:{0},Body:{1},错误信息:{2}",
                            ReplaceParameters = new List <object>()
                            {
                                response.RequestMessage.RequestUri.ToString(), requestBody, strContent
                            }
                        };

                        ex = new UtilityException((int)Errors.CrmWebApiCommonError, fragment);
                        break;
                    }

                    if (ex != null)
                    {
                        throw ex;
                    }

                    if (!canContinue)
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
        }
コード例 #15
0
        public async Task <RealWorkfolwActivityDescription> Execute(string activityConfiguration)
        {
            XElement element = null;

            try
            {
                element = XElement.Parse(activityConfiguration);
            }
            catch (Exception ex)
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.RealWorkfolwActivityConfigurationParseXMLError,
                    DefaultFormatting = "工作流活动配置转换成XML时出错,活动配置:{0},错误原因:{1}",
                    ReplaceParameters = new List <object>()
                    {
                        activityConfiguration, ex.Message
                    }
                };

                var exception = new UtilityException((int)Errors.RealWorkfolwActivityConfigurationParseXMLError, fragment);
                exception.Data[UtilityExceptionDataKeys.Catch] = true;
                throw exception;
            }

            var idAttribute = element.Attribute("id");

            if (idAttribute == null)
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.RealWorkfolwActivityConfigurationParseXMLError,
                    DefaultFormatting = "工作流活动配置转换成XML时出错,活动配置:{0},错误原因:{1}",
                    ReplaceParameters = new List <object>()
                    {
                        activityConfiguration, new TextFragment()
                        {
                            Code = TextCodes.RealWorkfolwActivityConfigurationMissXMLAttribute, DefaultFormatting = "工作流活动配置缺少XML属性{0}", ReplaceParameters = new List <object>()
                            {
                                "id"
                            }
                        }
                    }
                };

                var exception = new UtilityException((int)Errors.RealWorkfolwActivityConfigurationParseXMLError, fragment);
                exception.Data[UtilityExceptionDataKeys.Catch] = true;
                throw exception;
            }

            if (!Guid.TryParse(idAttribute.Value, out Guid id))
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.RealWorkfolwActivityConfigurationParseXMLError,
                    DefaultFormatting = "工作流活动配置转换成XML时出错,活动配置:{0},错误原因:{1}",
                    ReplaceParameters = new List <object>()
                    {
                        activityConfiguration, new TextFragment()
                        {
                            Code = TextCodes.RealWorkfolwActivityConfigurationIdAttributeParseError, DefaultFormatting = "工作流活动配置的Id属性不能转换成Guid,当前的Id属性为{0}", ReplaceParameters = new List <object>()
                            {
                                idAttribute.Value
                            }
                        }
                    }
                };

                var exception = new UtilityException((int)Errors.RealWorkfolwActivityConfigurationParseXMLError, fragment);
                exception.Data[UtilityExceptionDataKeys.Catch] = true;
                throw exception;
            }

            if (!_resolveFactories.TryGetValue(element.Name.ToString().ToLower(), out IFactory <IRealWorkfolwActivityResolve> resolveFactory))
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.NotFoundRealWorkfolwActivityResolveByName,
                    DefaultFormatting = "找不到名称为{0}的工作流活动解析",
                    ReplaceParameters = new List <object>()
                    {
                        element.Name.ToString().ToLower()
                    }
                };

                throw new UtilityException((int)Errors.NotFoundRealWorkfolwActivityResolveByName, fragment);
            }

            var description = await resolveFactory.Create().Execute(activityConfiguration);

            description.InputParameters = await _activityConfigurationService.ResolveActivityInputParameters(activityConfiguration);

            description.OutputParameters = await _activityConfigurationService.ResolveActivityOutputParameters(activityConfiguration);

            description.Name = element.Name.ToString().ToLower();
            description.Id   = id;
            return(description);
        }
コード例 #16
0
        public async Task <Dictionary <string, object> > Execute(RealWorkfolwActivityDescription activityDescription, RealWorkfolwContext context, Dictionary <string, object> runtimeParameters)
        {
            Exception exception;
            RealWorkfolwActivityDescriptionDataForCondition data = activityDescription.Data as RealWorkfolwActivityDescriptionDataForCondition;

            if (data == null)
            {
                string realType;
                if (activityDescription.Data == null)
                {
                    realType = "null";
                }
                else
                {
                    realType = activityDescription.Data.GetType().FullName;
                }

                var fragment = new TextFragment()
                {
                    Code = TextCodes.RealWorkfolwActivityDescriptionDataTypeNotMatch,
                    DefaultFormatting = "工作流活动描述的Data属性的类型不匹配,期待的类型为{0},实际类型为{1},发生位置:{2}",
                    ReplaceParameters = new List <object>()
                    {
                        typeof(RealWorkfolwActivityDescriptionDataForCondition).FullName, realType, $"{this.GetType().FullName}.Execute"
                    }
                };

                throw new UtilityException((int)Errors.RealWorkfolwActivityDescriptionDataTypeNotMatch, fragment);
            }

            if (!activityDescription.InputParameters.TryGetValue("condition", out RealWorkfolwActivityParameter conditionParameter))
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.NotFoundRealWorkfolwActivityDescriptionInputByName,
                    DefaultFormatting = "名称为{0}的工作流描述中找不到名称为{1}的输入参数",
                    ReplaceParameters = new List <object>()
                    {
                        activityDescription.Name, "condition"
                    }
                };

                exception = new UtilityException((int)Errors.NotFoundRealWorkfolwActivityDescriptionInputByName, fragment);
                exception.Data[UtilityExceptionDataKeys.Catch] = true;
                throw exception;
            }

            var conditionParameterResultObj = await _realWorkfolwActivityParameterHandle.Execute(conditionParameter, context);

            var conditionParameterResult = conditionParameterResultObj as bool?;

            if (conditionParameterResult == null)
            {
                string realType;
                if (conditionParameterResultObj == null)
                {
                    realType = null;
                }
                else
                {
                    realType = conditionParameterResultObj.GetType().FullName;
                }

                var fragment = new TextFragment()
                {
                    Code = TextCodes.RealWorkfolwActivityDescriptionInputResultTypeNotMatch,
                    DefaultFormatting = "名称为{0}的工作流描述中名称为{1}的输入参数的结果值类型不匹配,期待的类型为{2},实际类型为{3},参数配置为{4}",
                    ReplaceParameters = new List <object>()
                    {
                        activityDescription.Name, "condition", typeof(bool).FullName, realType, conditionParameter.Configuration
                    }
                };

                exception = new UtilityException((int)Errors.RealWorkfolwActivityDescriptionInputResultTypeNotMatch, fragment);
                exception.Data[UtilityExceptionDataKeys.Catch] = true;
                throw exception;
            }

            if (conditionParameterResult.Value)
            {
                await ExecuteMatch(data.Match, context);
            }
            else
            {
                bool executed = false;
                foreach (var elseifItem in data.ElseIfs)
                {
                    conditionParameterResultObj = await _realWorkfolwActivityParameterHandle.Execute(elseifItem.Condition, context);

                    conditionParameterResult = conditionParameterResultObj as bool?;
                    if (conditionParameterResult == null)
                    {
                        string realType;
                        if (conditionParameterResultObj == null)
                        {
                            realType = null;
                        }
                        else
                        {
                            realType = conditionParameterResultObj.GetType().FullName;
                        }

                        var fragment = new TextFragment()
                        {
                            Code = TextCodes.RealWorkfolwActivityDescriptionInnerInputResultTypeNotMatch,
                            DefaultFormatting = "名称为{0}的工作流描述中内部输入参数{1}的计算结果类型不匹配,期待类型为{2},实际类型为{3},参数配置为{4}",
                            ReplaceParameters = new List <object>()
                            {
                                activityDescription.Name, elseifItem.Condition.Name, typeof(bool).FullName, realType, elseifItem.Condition.Configuration
                            }
                        };

                        exception = new UtilityException((int)Errors.RealWorkfolwActivityDescriptionInnerInputResultTypeNotMatch, fragment);
                        exception.Data[UtilityExceptionDataKeys.Catch] = true;
                        throw exception;
                    }

                    if (conditionParameterResult.Value)
                    {
                        await ExecuteMatch(elseifItem.Match, context);

                        executed = true;
                        break;
                    }
                }

                if (!executed)
                {
                    await ExecuteMatch(data.Else, context);
                }
            }

            Dictionary <string, object> outputDict = new Dictionary <string, object>();

            foreach (var outputItem in activityDescription.OutputParameters)
            {
                var outputResult = await _realWorkfolwActivityParameterHandle.Execute(outputItem.Value, context);

                outputDict[outputItem.Key] = outputResult;
            }

            return(outputDict);
        }
コード例 #17
0
        public async Task <RealWorkfolwActivityDescription> Execute(string activityConfiguration)
        {
            XElement configuration = null;

            try
            {
                configuration = XElement.Parse(activityConfiguration);
            }
            catch (Exception ex)
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.RealWorkfolwActivityConfigurationParseXMLError,
                    DefaultFormatting = "工作流活动配置转换成XML时出错,活动配置:{0},错误原因:{1}",
                    ReplaceParameters = new List <object>()
                    {
                        activityConfiguration, ex.Message
                    }
                };

                var exception = new UtilityException((int)Errors.RealWorkfolwActivityConfigurationParseXMLError, fragment);
                exception.Data[UtilityExceptionDataKeys.Catch] = true;
                throw exception;
            }

            RealWorkfolwActivityDescriptionDataForCondition data = new RealWorkfolwActivityDescriptionDataForCondition();


            var matchElement = configuration.Element("match");

            if (matchElement == null)
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.RealWorkfolwActivityConfigurationParseXMLError,
                    DefaultFormatting = "工作流活动配置转换成XML时出错,活动配置:{0},错误原因:{1}",
                    ReplaceParameters = new List <object>()
                    {
                        activityConfiguration, new TextFragment()
                        {
                            Code = TextCodes.RealWorkfolwActivityConfigurationMissXMLElement, DefaultFormatting = "工作流活动配置缺少XML节点{0}", ReplaceParameters = new List <object>()
                            {
                                "match"
                            }
                        }
                    }
                };

                var exception = new UtilityException((int)Errors.RealWorkfolwActivityConfigurationParseXMLError, fragment);
                exception.Data[UtilityExceptionDataKeys.Catch] = true;
                throw exception;
            }

            var matchActivities = await _activityConfigurationService.SeparateActivities(matchElement.ToString());

            List <RealWorkfolwActivityDescription> matchList = new List <RealWorkfolwActivityDescription>();

            foreach (var itemMatchActivity in matchActivities)
            {
                var match = await _realWorkfolwActivityResolve.Execute(itemMatchActivity.Configuration);

                matchList.Add(match);
            }

            data.Match   = matchList;
            data.ElseIfs = await GetElseifs(activityConfiguration, configuration);

            var elseElement = configuration.Element("else");

            if (elseElement == null)
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.RealWorkfolwActivityConfigurationParseXMLError,
                    DefaultFormatting = "工作流活动配置转换成XML时出错,活动配置:{0},错误原因:{1}",
                    ReplaceParameters = new List <object>()
                    {
                        activityConfiguration, new TextFragment()
                        {
                            Code = TextCodes.RealWorkfolwActivityConfigurationMissXMLElement, DefaultFormatting = "工作流活动配置缺少XML节点{0}", ReplaceParameters = new List <object>()
                            {
                                "else"
                            }
                        }
                    }
                };

                var exception = new UtilityException((int)Errors.RealWorkfolwActivityConfigurationParseXMLError, fragment);
                exception.Data[UtilityExceptionDataKeys.Catch] = true;
                throw exception;
            }

            var elseActivities = await _activityConfigurationService.SeparateActivities(elseElement.ToString());

            List <RealWorkfolwActivityDescription> elseList = new List <RealWorkfolwActivityDescription>();

            foreach (var itemElseActivity in elseActivities)
            {
                var elseActivity = await _realWorkfolwActivityResolve.Execute(itemElseActivity.Configuration);

                elseList.Add(elseActivity);
            }

            data.Else = elseList;


            RealWorkfolwActivityDescription description = new RealWorkfolwActivityDescription()
            {
                Data = data
            };

            return(description);
        }
コード例 #18
0
        public async Task <RealWorkfolwActivityDescription> Execute(string activityConfiguration)
        {
            XElement configuration = null;

            try
            {
                configuration = XElement.Parse(activityConfiguration);
            }
            catch (Exception ex)
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.RealWorkfolwActivityConfigurationParseXMLError,
                    DefaultFormatting = "工作流活动配置转换成XML时出错,活动配置:{0},错误原因:{1}",
                    ReplaceParameters = new List <object>()
                    {
                        activityConfiguration, ex.Message
                    }
                };
                var exception = new UtilityException((int)Errors.RealWorkfolwActivityConfigurationParseXMLError, fragment);
                exception.Data[UtilityExceptionDataKeys.Catch] = true;
                throw exception;
            }

            RealWorkfolwActivityDescriptionDataForResult data = new RealWorkfolwActivityDescriptionDataForResult();

            data.ResultParameters = new List <RealWorkfolwActivityParameter>();

            var setElement = configuration.Element("set");

            if (setElement == null)
            {
                var fragment = new TextFragment()
                {
                    Code = TextCodes.RealWorkfolwActivityConfigurationParseXMLError,
                    DefaultFormatting = "工作流活动配置转换成XML时出错,活动配置:{0},错误原因:{1}",
                    ReplaceParameters = new List <object>()
                    {
                        activityConfiguration, new TextFragment()
                        {
                            Code = TextCodes.RealWorkfolwActivityConfigurationMissXMLElement, DefaultFormatting = "工作流活动配置缺少XML节点{0}", ReplaceParameters = new List <object>()
                            {
                                "set"
                            }
                        }
                    }
                };

                var exception = new UtilityException((int)Errors.RealWorkfolwActivityConfigurationParseXMLError, fragment);
                exception.Data[UtilityExceptionDataKeys.Catch] = true;
                throw exception;
            }

            var parameterElements = setElement.Elements("parameter");

            foreach (var parameterElement in parameterElements)
            {
                var parameter = await _parameterConfigurationService.ResolveParameter(parameterElement.ToString());

                data.ResultParameters.Add(parameter);
            }

            RealWorkfolwActivityDescription description = new RealWorkfolwActivityDescription()
            {
                Data = data
            };

            return(description);
        }
コード例 #19
0
ファイル: ServiceCompile.cs プロジェクト: gyb333/KDS3
        /// <summary>
        /// 完成编译(系统自动调用)
        /// </summary>
        public override void EndCompile()
        {
            try
            {
                var compiler   = new CSharpCodeProvider();
                var parameters = new CompilerParameters()
                {
                    GenerateExecutable      = false,
                    GenerateInMemory        = false,
                    IncludeDebugInformation = true,
                    OutputAssembly          = Path.Combine(ProjectPath, string.Format("Wilmar.Project.{0}.dll", this.Project.Identity.ToUpper()))
                };
                parameters.TempFiles.KeepFiles = true;
                parameters.ReferencedAssemblies.AddRange(ReferencedAssemblies);
                if (ExtensionDirecotry.Exists)
                {
                    parameters.ReferencedAssemblies.Add(ExtensionDirecotry.FullName + "\\Wilmar.Interface.Common.dll");

                    foreach (var file in ExtensionDirecotry.GetFiles($"Wilmar.Custom.{Project.Identity.ToUpper()}*.dll"))
                    {
                        if (Regex.IsMatch(file.Name, $"Wilmar\\.Custom\\.{Project.Identity.ToUpper()}\\d+\\.dll"))
                        {
                            parameters.ReferencedAssemblies.Add(file.FullName);
                        }
                    }
                }
                CompilerResults result = null;
                if (IsFileCompile)
                {
                    List <string> filenames = new List <string>();
                    string        root      = Path.Combine(Path.GetTempPath(), "PlatformGenerateCode", Project.Identity + DateTime.Now.Ticks.ToString());
                    if (Directory.Exists(root))
                    {
                        Directory.Delete(root, true);
                    }
                    foreach (var item in CompileFiles)
                    {
                        var file = new FileInfo(Path.Combine(root, item.Category, item.Name + ".cs"));
                        if (!file.Directory.Exists)
                        {
                            file.Directory.Create();
                        }
                        File.WriteAllText(file.FullName, item.Content);
                        filenames.Add(file.FullName);
                    }
                    result = compiler.CompileAssemblyFromFile(parameters, filenames.ToArray());
                }
                else
                {
                    result = compiler.CompileAssemblyFromSource(parameters, CompileFiles.Select(a => a.Content).ToArray());
                }

                if (result.Errors.Count > 0)
                {
                    StringBuilder errsb = new StringBuilder();
                    foreach (CompilerError item in result.Errors)
                    {
                        if (!item.IsWarning)
                        {
                            errsb.AppendLine(string.Format("{0} {1}", item.FileName, item.ErrorText));
                        }
                    }
                    throw UtilityException.Compile(errsb.ToString());
                }
                else
                {
                    File.WriteAllText(
                        Path.Combine(ProjectPath,
                                     string.Format("Wilmar.Project.{0}.configure",
                                                   this.Project.Identity.ToUpper())),
                        Utility.Serialize(Configure),
                        Encoding.UTF8);
                    Permission.Commit();
                }
            }
            catch (Exception ex)
            {
                throw UtilityException.Compile(ex.Message);
            }
        }