常用动态调用webservice的方法

   class WebServicesH
    {


        public static void getMethod()
        {
            //服务地址,该地址可以放到程序的配置文件中,这样即使服务地址改变了,也无须重新编译程序。
            string url = "http://localhost:4172/WebServiceDemo.asmx";
 
            //客户端代理服务命名空间,可以设置成需要的值。
            string ns = string.Format("ServiceReference1");
            //获取WSDL
            WebClient wc = new WebClient();
            Stream stream = wc.OpenRead(url + "?WSDL");
            ServiceDescription sd = ServiceDescription.Read(stream);//服务的描述信息都可以通过ServiceDescription获取
            string classname = sd.Services[0].Name;
 
            ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
            sdi.AddServiceDescription(sd, "", "");
            CodeNamespace cn = new CodeNamespace(ns);
 
            //生成客户端代理类代码
            CodeCompileUnit ccu = new CodeCompileUnit();
            ccu.Namespaces.Add(cn);
            sdi.Import(cn, ccu);
            CSharpCodeProvider csc = new CSharpCodeProvider();
 
            //设定编译参数
            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 = csc.CompileAssemblyFromDom(cplist, ccu);
            if (cr.Errors.HasErrors == true)
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
                {
                    sb.Append(ce.ToString());
                    sb.Append(System.Environment.NewLine);
                }
                throw new Exception(sb.ToString());
            }
 
            //生成代理实例,并调用方法
            Assembly assembly = cr.CompiledAssembly;
            Type t = assembly.GetType(ns + "." + classname, true, true);
            object obj = Activator.CreateInstance(t);


            //调用HelloWorld方法
            MethodInfo helloWorld = t.GetMethod("HelloWorld");
            object helloWorldReturn = helloWorld.Invoke(obj, null);
            Console.WriteLine("调用HelloWorld方法,返回{0}", helloWorldReturn.ToString());

            //获取Add方法的参数
            ParameterInfo[] helloWorldParamInfos = helloWorld.GetParameters();
            Console.WriteLine("HelloWorld方法有{0}个参数:", helloWorldParamInfos.Length);
            foreach (ParameterInfo paramInfo in helloWorldParamInfos)
            {
                Console.WriteLine("参数名:{0},参数类型:{1}", paramInfo.Name, paramInfo.ParameterType.Name);
            }
 
            //获取HelloWorld返回的数据类型
            string helloWorldReturnType = helloWorld.ReturnType.Name;
            Console.WriteLine("HelloWorld返回的数据类型是{0}", helloWorldReturnType);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值