首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web Service >

动态调用webservice,怎么接受out返回值

2012-03-09 
动态调用webservice,如何接受out返回值。问题是这样的。客户端不是直接调用webservice,中间还有个代理类,这

动态调用webservice,如何接受out返回值。
问题是这样的。
客户端不是直接调用webservice,中间还有个代理类,这个类负责动态编译调用,webservice。
客户端代码

C# code
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using ShangWu168.Tools;using ShangWu168.Service.Dynamic;using System.Data;using System.Diagnostics;using System.Runtime.Serialization.Formatters.Binary;using System.IO;namespace ShangWu168.Web{    public partial class test : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            this.InitialList();        }         private void InitialList()        {                     int intCurrentPage = RequestClass.GetQueryInt("page", 1);//当前页            int PageSize = RequestClass.GetQueryInt("PageSize", 15);//每页条数            string PageCount = "0";//这个地方想接收webservice返回的out值            string RowsCount = "0";//这个地方想接收webservice返回的out            string strUrl = "http://127.0.0.1:78/Pagination.asmx";            string[] args = new string[10];            args[0] = "web_member";            args[1] = "member_id";            args[2] = "*";            args[3] = "desc";            args[4] = "member_id";            args[5] = intCurrentPage.ToString();            args[6] = PageSize.ToString();            args[7] = "member_id>1";            args[8] = PageCount;            args[9] = RowsCount;            Stopwatch sw = new Stopwatch();            sw.Start();            byte[] zipBuffer = (byte[])WebServiceDynamicInvoke.InvokeWebService(strUrl, "Pagination", "GetDataSetSurrogateZipBytes", args); ;            byte[] buffer = UnZip.Decompress(zipBuffer);            BinaryFormatter bf = new BinaryFormatter();            DataSetSurrogate dss = bf.Deserialize(new MemoryStream(buffer)) as DataSetSurrogate;            DataSet ds = dss.ConvertToDataSet();            GridView1.DataSource = ds.Tables[0].DefaultView;            GridView1.DataBind();            sw.Stop();        }    }}

代理类代码
C# code
using System;using System.Collections.Generic;using System.Text;using System.Net;using System.IO;using Microsoft.CSharp;using System.CodeDom.Compiler;using System.CodeDom;using System.Web.Services.Description;namespace ShangWu168.Service.Dynamic{    public class WebServiceDynamicInvoke    {        /// <summary>        /// 动态调用WebService        /// </summary>        /// <param name="url">WebService地址</param>        /// <param name="methodname">方法名(模块名)</param>        /// <param name="args">参数列表</param>        /// <returns>object</returns>        public static object InvokeWebService(string url, string methodname, object[] args)        {            return InvokeWebService(url, null, methodname, args);        }        /// <summary>        /// 动态调用WebService        /// </summary>        /// <param name="url">WebService地址</param>        /// <param name="classname">类名</param>        /// <param name="methodname">方法名(模块名)</param>        /// <param name="args">参数列表</param>        /// <returns>object</returns>        public static object InvokeWebService(string url, string classname, string methodname, object[] args)        {            string @namespace = "ServiceBase.WebService.DynamicWebLoad";            if (classname == null || classname == "")            {                classname = WebServiceDynamicInvoke.GetClassName(url);            }            //获取服务描述语言(WSDL)            WebClient wc = new WebClient();            Stream stream = wc.OpenRead(url + "?WSDL");            ServiceDescription sd = ServiceDescription.Read(stream);            ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();            sdi.AddServiceDescription(sd, "", "");            CodeNamespace cn = new CodeNamespace(@namespace);            //生成客户端代理类代码            CodeCompileUnit ccu = new CodeCompileUnit();            ccu.Namespaces.Add(cn);            sdi.Import(cn, ccu);            CSharpCodeProvider csc = new CSharpCodeProvider();            ICodeCompiler icc = csc.CreateCompiler();            //设定编译器的参数            CompilerParameters cplist = new CompilerParameters();            cplist.GenerateExecutable = false;            cplist.GenerateInMemory = true;            cplist.ReferencedAssemblies.Add("System.dll");            cplist.ReferencedAssemblies.Add("System.XML.dll");            cplist.ReferencedAssemblies.Add("System.Web.Services.dll");            cplist.ReferencedAssemblies.Add("System.Data.dll");            //编译代理类            CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);            if (true == cr.Errors.HasErrors)            {                System.Text.StringBuilder sb = new StringBuilder();                foreach (CompilerError ce in cr.Errors)                {                    sb.Append(ce.ToString());                    sb.Append(System.Environment.NewLine);                }                throw new Exception(sb.ToString());            }            //生成代理实例,并调用方法            System.Reflection.Assembly assembly = cr.CompiledAssembly;            Type t = assembly.GetType(@namespace + "." + classname, true, true);            object obj = Activator.CreateInstance(t);            System.Reflection.MethodInfo mi = t.GetMethod(methodname);            return mi.Invoke(obj, args);//这里不知道如何接受webservice的out值        }        /// <summary>        /// 解析url获取类名        /// </summary>        /// <param name="url">url地址</param>        /// <returns>string</returns>        private static string GetClassName(string url)        {            string[] parts = url.Split('/');            string[] pps = parts[parts.Length - 1].Split('.');            return pps[0];        }    }} 




[解决办法]
C# code
 public static object InvokeWebService(string url, string classname, string methodname, object[] args,params Type[] types){    …………………… System.Reflection.MethodInfo mi = null;            if (types.Length == 0)                mi=t.GetMethod(methodname);            else                mi=t.GetMethod(methodname, types);            return mi.Invoke(obj, args);//这里不知道如何接受webservice的out值}调用你另外一个贴子中4楼的helloworld方法            string msg;            string result=string.Empty;            object[] args=new object[]{result};            Type[] type=new Type[]{Type.GetType("System.String&")};            msg= WebServiceDynamicInvoke.InvokeWebService("http://localhost:10189/web1/WebService.asmx",null, "HelloWorld", args,type).ToString();            MessageBox.Show(msg);            MessageBox.Show(args[0].ToString()); 

热点排行