zoukankan      html  css  js  c++  java
  • 【转载】动态调用WebService

    好像很多人做WebService的时候都是直接添加引用的方式,然后调用服务端的方法.这样就个问题,就是每次我服务端添加了方法或者修改了方法后都要更新Web引用,这样比较麻烦.下面给一个不用添加引用的方式调用服务端的方法.只是一个简单的测试,不是很规范,用得着的人可以自己封装一下,然后直接传服务端的方法名进去,Type.GetMethod获取方法,然后method.Invoke返回结果

    高手些多多包函,主要是给用得着的人参考一下,互相学习.代码主要是用了 System.Web.Services.Description里的东西

    新建一个WebService项目,添加以下代码:

    代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;

    namespace TestWebService
    {
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/",Description="我的Web服务")]
    [WebServiceBinding(ConformsTo
    = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(
    false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class TestWebService : System.Web.Services.WebService
    {

    [WebMethod]
    public string HelloWorld()
    {
    return "测试Hello World";
    }

    [WebMethod]
    public string Test()
    {
    return "测试Test";
    }

    [WebMethod(CacheDuration
    = 60,Description = "测试")]
    public List<String> GetPersons()
    {
    List
    <String> list = new List<string>();
    list.Add(
    "测试一");
    list.Add(
    "测试二");
    list.Add(
    "测试三");
    return list;
    }

    }

    }

     下面是客户端:

    代码
    1
    2
    3
    4
    5  using System;
    6  using System.IO;
    7  using System.Collections.Generic;
    8  using System.Linq;
    9  using System.Collections;
    10  using System.Web;
    11  using System.Net;
    12  using System.Reflection;
    13  using System.CodeDom;
    14  using System.CodeDom.Compiler;
    15  using System.Web.Services;
    16  using System.Text;
    17 using System.Web.Services.Description;
    18 using System.Web.Services.Protocols;
    19 using System.Xml.Serialization;
    20 using System.Windows.Forms;
    21
    22 namespace ConsoleApplication1
    23 {
    24 class Program
    25 {
    26 static void Main(string[] args)
    27 {
    28 WebClient client = new WebClient();
    29 String url = "http://localhost:3182/Service1.asmx?WSDL";//这个地址可以写在Config文件里面,这里取出来就行了.在原地址后面加上: ?WSDL
    30 Stream stream = client.OpenRead(url);
    31 ServiceDescription description = ServiceDescription.Read(stream);
    32
    33 ServiceDescriptionImporter importer = new ServiceDescriptionImporter();//创建客户端代理代理类。
    34
    35 importer.ProtocolName = "Soap"; //指定访问协议。
    36 importer.Style = ServiceDescriptionImportStyle.Client; //生成客户端代理。
    37 importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;
    38
    39 importer.AddServiceDescription(description, null, null); //添加WSDL文档。
    40
    41 CodeNamespace nmspace = new CodeNamespace(); //命名空间
    42 nmspace.Name = "TestWebService";
    43 CodeCompileUnit unit = new CodeCompileUnit();
    44 unit.Namespaces.Add(nmspace);
    45
    46 ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);
    47 CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
    48
    49 CompilerParameters parameter = new CompilerParameters();
    50 parameter.GenerateExecutable = false;
    51 parameter.OutputAssembly = "MyTest.dll";//输出程序集的名称
    52 parameter.ReferencedAssemblies.Add("System.dll");
    53 parameter.ReferencedAssemblies.Add("System.XML.dll");
    54 parameter.ReferencedAssemblies.Add("System.Web.Services.dll");
    55 parameter.ReferencedAssemblies.Add("System.Data.dll");
    56
    57 CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);
    58 if (result.Errors.HasErrors)
    59 {
    60 // 显示编译错误信息
    61 }
    62
    63 Assembly asm = Assembly.LoadFrom("MyTest.dll");//加载前面生成的程序集
    64 Type t = asm.GetType("TestWebService.TestWebService");
    65
    66 object o = Activator.CreateInstance(t);
    67 MethodInfo method = t.GetMethod("GetPersons");//GetPersons是服务端的方法名称,你想调用服务端的什么方法都可以在这里改,最好封装一下
    68 String[] item = (String[])method.Invoke(o, null);
    69 //注:method.Invoke(o, null)返回的是一个Object,如果你服务端返回的是DataSet,这里也是用(DataSet)method.Invoke(o, null)转一下就行了
    70 foreach (string str in item)
    71 Console.WriteLine(str);
    72
    73 //上面是根据WebService地址,模似生成一个代理类,如果你想看看生成的代码文件是什么样子,可以用以下代码保存下来,默认是保存在bin目录下面
    74 TextWriter writer = File.CreateText("MyTest.cs");
    75 provider.GenerateCodeFromCompileUnit(unit, writer, null);
    76 writer.Flush();
    77 writer.Close();
    78 }
    79 }
    80 }
    81
    其实也是根据地址进行解析,模似生成一个代理类的dll,然后再反射调用这个dll的方法.可以把生成的保存下来看看,呵呵
    希望对大家有用。
  • 相关阅读:
    去掉Form产生的空行
    转:Override错误
    面试
    JMF获取设备列表失败,获取视频设备失败?
    jquery 插件ztree的应用动态加载树节点数据
    关于Struts2上传文件
    未能解析引用的程序集“”,因为它对不在当前目标框架“”具有依赖关系。请删除对不在目标框架中的程序集的引用,或考虑重新确定项目的目标。 Kevin
    The diffrence between Cast() and OfType() Kevin
    “System.Collections.Generic.IEnumerable<decimal>”不包含“ToArray”的定义,并且找不到可接受类型为“System.Collections.Generic.IEnumerable<decimal>”的第一个参数的扩展方法“ToArray” Kevin
    partial关键字的含义和使用 Kevin
  • 原文地址:https://www.cnblogs.com/wbpmrck/p/1820501.html
Copyright © 2011-2022 走看看