Exemplo n.º 1
0
        /// <summary>
        /// 动态调用web服务
        /// </summary>
        /// <param name="url">服务地址</param>
        /// <param name="@namespace">默认命名空间</param>
        /// <param name="methodname">方法名称</param>
        /// <param name="args">参数列表</param>
        /// <returns></returns>
        public static void InvokeWebService(string url, string @namespace, string methodname, object[] args, ASyncResultCallback resultcallback)
        {
            CancellableTask task = new CancellableTask(new CancellableTask.WorkCallback((object obj) =>
            {
                try
                {
                    return(InvokeWebService(url, @namespace, null, methodname, args));
                }
                catch (Exception exception) { return("请求地址不存在或请求参数错误," + exception.Message); }
            }), new CancellableTask.CancelCallback((object obj) =>
            {
                object result = null; result = "请求被取消"; resultcallback(result);
            }));

            task.BeginInvoke(new object[] { url, methodname, args }, new AsyncCallback((IAsyncResult iresult) =>
            {
                object result = null;
                try
                {
                    result = task.EndInvoke(iresult);
                }
                catch { result = "请求超时"; }
                resultcallback.Invoke(result);
            }), null, 15 * 1000);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 动态调用web服务
        /// </summary>
        /// <param name="url">服务地址</param>
        /// <param name="@namespace">默认命名空间</param>
        /// <param name="methodname">方法名称</param>
        /// <param name="args">参数列表</param>
        /// <returns></returns>
        public static string InvokeWebService(string url, string @namespace, string methodname, object[] args)
        {
            string reqVal = string.Empty;

            try
            {
                CancellableTask.WorkCallback workCallBack = delegate(object obj)
                {
                    object val = "";
                    try
                    {
                        val = InvokeWebService(url, @namespace, null, methodname, args);
                    }
                    catch (Exception exception)
                    {
                        SimpleConsole.WriteLine(exception);
                        val = "请求地址不存在或参数错误";
                    }
                    reqVal = val.ToString();
                    return(val);
                };
                CancellableTask.CancelCallback cacelCallBack = delegate(object obj)
                {
                    reqVal = "请求超时,已被取消";
                };
                CancellableTask task = new CancellableTask(workCallBack, cacelCallBack);

                AsyncCallback asyncCallback = delegate(IAsyncResult obj)
                {
                    try { task.EndInvoke(obj); } catch { }
                    SimpleConsole.WriteLine("AsyncCallback");
                };

                var result = task.BeginInvoke(null, asyncCallback, null, 15 * 1000);
                try
                {
                    object endResult = task.EndInvoke(result);
                    SimpleConsole.WriteLine(endResult);
                }
                catch (Exception exception)
                {
                    SimpleConsole.WriteLine(exception);
                }
            }
            catch (Exception exception)
            {
                reqVal = exception.Message;
            }
            return(reqVal);
        }