Example #1
0
 private static void MergeVariablesToApplicationRuntimeParameters(WfApplicationRuntimeParameters runtimeParameters, WfVariableDescriptorCollection variables)
 {
     foreach (WfVariableDescriptor variable in variables)
     {
         runtimeParameters[variable.Key] = variable.ActualValue;
     }
 }
        private HttpWebRequest GenerateWebRequestObj(TimeSpan timeout, WfApplicationRuntimeParameters context)
        {
            ExceptionHelper.TrueThrow(string.IsNullOrEmpty(this._SvcOperationDef.AddressDef.Address), "服务地址定义不能为空.");
            ExceptionHelper.TrueThrow(string.IsNullOrEmpty(this._SvcOperationDef.OperationName), "调用方法名称不能为空.");

            HttpWebRequest request;

            switch (this._SvcOperationDef.AddressDef.RequestMethod)
            {
            case WfServiceRequestMethod.Get:
                request = this.CreateGetRequest(timeout, context);
                break;

            case WfServiceRequestMethod.Post:
                request = this.CreatePostRequest(timeout, context);
                break;

            case WfServiceRequestMethod.Soap:
                request = this.CreateSoapRequest(timeout);
                break;

            default:
                throw new NotImplementedException();
            }
            ;

            if (this._SvcOperationDef.AddressDef.Credential != null)
            {
                request.Credentials = (NetworkCredential)this._SvcOperationDef.AddressDef.Credential;
            }

            return(request);
        }
        private HttpWebRequest CreatePostRequest(TimeSpan timeout, WfApplicationRuntimeParameters context)
        {
            HttpWebRequest result = (HttpWebRequest)HttpWebRequest.Create(FormatUrl(_SvcOperationDef.AddressDef.Address, true)
                                                                          + HttpUtility.UrlEncode(_SvcOperationDef.OperationName));

            result.Method          = "POST";
            result.Timeout         = (int)timeout.TotalMilliseconds;
            result.KeepAlive       = false;
            result.ProtocolVersion = HttpVersion.Version10;
            result.Headers.CopyFrom(this.Headers);

            string postData = string.Empty;

            switch (_SvcOperationDef.AddressDef.ContentType)
            {
            case WfServiceContentType.Form:
                result.ContentType = "application/x-www-form-urlencoded";
                postData           = this.CreateQueryString(context);
                break;

            case WfServiceContentType.Json:
                result.ContentType = "application/json";
                postData           = this.CreateJsonData(context);
                break;

            default: break;
            }

            AttachDataToRequest(result, postData);
            return(result);
        }
        public WfServiceInvoker(WfServiceOperationDefinition svcOperationDef, WfApplicationRuntimeParameters invokeContext)
        {
            this._SvcOperationDef = svcOperationDef;
            this._InvokeContext   = invokeContext;

            this.InitConnectionMappings();
            this.InitHeaders();
        }
Example #5
0
        private static bool InnerGetValueRecursively <T>(WfApplicationRuntimeParameters parameters, IWfProcess process, WfProbeApplicationRuntimeParameterMode probeMode, string paramName, out object result)
        {
            paramName.CheckStringIsNullOrEmpty("paramName");

            result = null;
            bool existed = false;

            if (parameters.Contains(paramName))
            {
                object dataInARP = parameters[paramName];

                if (dataInARP is T)
                {
                    result = (T)dataInARP;
                }
                else
                {
                    result = DataConverter.ChangeType(parameters[paramName], typeof(T));
                }

                existed = true;
            }
            else
            {
                if (process != null)
                {
                    bool probParent = false;

                    switch (probeMode)
                    {
                    case WfProbeApplicationRuntimeParameterMode.Auto:
                        probParent = process.Descriptor.Properties.GetValue("ProbeParentProcessParams", false);
                        break;

                    case WfProbeApplicationRuntimeParameterMode.NotRecursively:
                        probParent = false;
                        break;

                    case WfProbeApplicationRuntimeParameterMode.Recursively:
                        probParent = true;
                        break;
                    }

                    if (probParent && process.HasParentProcess)
                    {
                        existed = InnerGetValueRecursively <T>(process.EntryInfo.OwnerActivity.Process.ApplicationRuntimeParameters, process.EntryInfo.OwnerActivity.Process, probeMode, paramName, out result);
                    }
                }
            }

            return(existed);
        }
        private string CreateJsonData(WfApplicationRuntimeParameters context)
        {
            string result = string.Empty;

            if (_SvcOperationDef.Params.Count > 0)
            {
                Dictionary <string, object> jsonDict = new Dictionary <string, object>();

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

                foreach (string key in this.Headers.AllKeys)
                {
                    headers[key] = this.Headers[key];
                }

                jsonDict["__Headers"]            = headers;
                jsonDict["__ConnectionMappings"] = this.ConnectionMappings;

                foreach (var item in this._SvcOperationDef.Params)
                {
                    if (item.Type == WfSvcOperationParameterType.RuntimeParameter)
                    {
                        var paraName = item.Value != null?item.Value.ToString() : string.Empty;

                        if (paraName.IsNullOrEmpty())
                        {
                            paraName = item.Name;               //流程运行时参数名与方法参数名相同
                        }
                        var paramValue = context.GetValueRecursively <object>(paraName, null);

                        if (paramValue == null)
                        {
                            throw new ArgumentException("未能在CurrentProcess.ApplicationRuntimeParameters中找到参数" + paraName);
                        }

                        jsonDict.Add(item.Name, paramValue);
                    }
                    else
                    {
                        jsonDict.Add(item.Name, item.GetDeserializedValue());
                    }
                }

                WfConverterHelper.RegisterConverters();

                result = JSONSerializerExecute.Serialize(jsonDict);
            }

            return(result);
        }
        internal static string GetCurrentAdministrativeUnitCodeName(IWfProcess process)
        {
            WfApplicationRuntimeParameters runtimeParameters = null;

            if (WfApplicationParametersContext.Current != null)
            {
                runtimeParameters = WfApplicationParametersContext.Current.ApplicationRuntimeParameters;
            }

            if (runtimeParameters == null && process != null)
            {
                runtimeParameters = process.ApplicationRuntimeParameters;
            }

            return(runtimeParameters.GetValueRecursively(AdministrativeUnitParameterName, string.Empty));
        }
        private HttpWebRequest CreateGetRequest(TimeSpan timeout, WfApplicationRuntimeParameters context)
        {
            string url = string.Concat(FormatUrl(this._SvcOperationDef.AddressDef.Address, true),
                                       HttpUtility.UrlEncode(this._SvcOperationDef.OperationName));

            string strQuery = this.CreateQueryString(context);

            if (strQuery.IsNotEmpty())
            {
                url = string.Format("{0}?{1}", url, strQuery);
            }

            HttpWebRequest result = (HttpWebRequest)HttpWebRequest.Create(url);

            result.Timeout         = (int)timeout.TotalMilliseconds;
            result.Method          = "GET";
            result.ContentType     = "text/xml";
            result.KeepAlive       = false;
            result.ProtocolVersion = HttpVersion.Version10;
            result.Headers.CopyFrom(this.Headers);

            return(result);
        }
        /// <summary>
        /// 构造请求字符串
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private string CreateQueryString(WfApplicationRuntimeParameters context)
        {
            StringBuilder result = new StringBuilder();

            foreach (var item in this._SvcOperationDef.Params)
            {
                if (result.Length > 0)
                {
                    result.Append("&");
                }

                result.Append(HttpUtility.UrlEncode(item.Name));
                result.Append("=");

                if (item.Type == WfSvcOperationParameterType.RuntimeParameter)
                {
                    var paraName = item.Value != null?item.Value.ToString() : "";

                    if (string.IsNullOrEmpty(paraName))
                    {
                        paraName = item.Name;           //流程运行时参数名与方法参数名相同
                    }
                    string paraVal = context.GetValueRecursively(paraName, string.Empty);

                    result.Append(HttpUtility.UrlEncode(paraVal));
                }
                else
                {
                    if (item.Value != null)
                    {
                        result.Append(HttpUtility.UrlEncode(item.Value.ToString()));
                    }
                }
            }

            return(result.ToString());
        }
        /// <summary>
        /// 调用服务
        /// </summary>
        /// <param name="timeout">请求超时时间,单位毫秒</param>
        /// <param name="context">上下文参数</param>
        /// <returns></returns>
        public object Invoke(TimeSpan timeout, WfApplicationRuntimeParameters context)
        {
            try
            {
                if (context == null)
                {
                    context = WfServiceInvoker.InvokeContext;
                }

                HttpWebRequest request = GenerateWebRequestObj(timeout, context);

                try
                {
                    using (WebResponse response = request.GetResponse())
                    {
                        using (Stream stream = response.GetResponseStream())
                        {
                            object result = null;

                            if (stream != null)
                            {
                                StreamReader streamReader = new StreamReader(stream, Encoding.UTF8);
                                string       rtnContent   = streamReader.ReadToEnd();

                                result = ParseServiceResultToObject(rtnContent);

                                if (result == null)
                                {
                                    result = ExceptionHelper.DoSilentFunc(() => JSONSerializerExecute.DeserializeObject(rtnContent), rtnContent);

                                    if (result is WfErrorDTO)
                                    {
                                        string errorMessage = ((WfErrorDTO)result).ToString() + Environment.NewLine + request.RequestUri.ToString();

                                        throw new WfServiceInvokeException(((WfErrorDTO)result).ToString());
                                    }
                                }
                            }

                            if (result is IDictionary <string, object> )
                            {
                                foreach (KeyValuePair <string, object> kp in (IDictionary <string, object>)result)
                                {
                                    context[kp.Key] = kp.Value;
                                }
                            }

                            if (this._SvcOperationDef.RtnXmlStoreParamName.IsNotEmpty())
                            {
                                context[this._SvcOperationDef.RtnXmlStoreParamName] = result;
                            }

                            return(result);
                        }
                    }
                }
                catch (WebException ex)
                {
                    if (ex.Response == null)
                    {
                        throw new WfServiceInvokeException(string.Format("调用服务时发生了异常,{0},但无响应内容。HTTP状态为{1}", ex.Message, ex.Status), ex);
                    }
                    else
                    {
                        throw WfServiceInvokeException.FromWebResponse(ex.Response);
                    }
                }
            }
            catch (WebException ex)
            {
                throw new WfServiceInvokeException(ex.Message, ex);
            }
        }
 /// <summary>
 /// 调用服务,默认超时30秒
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 public object Invoke(WfApplicationRuntimeParameters context)
 {
     return(Invoke(this._SvcOperationDef.Timeout, context));
 }