博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

动态webservice和静态webservice

Posted on 2008-12-20 18:47  codingsilence  阅读(385)  评论(0编辑  收藏  举报

一、普通的静态调用方法,可更换Url地址

 

localhost.WebService ws = new localhost.WebService();
ws.Url = "new url";
ws.HelloWorld ();

 

二、动态调用,性能较低

 

 

  1. ///<summary>
  2.         ///动态Web服务代理方法
  3.         ///</summary>
  4.         ///<param name="@namespace">传入空间命名</param>
  5.         ///<param name="url">传入URL参数</param>
  6.         ///<param name="classname">传入类名</param>
  7.         ///<param name="methodname">传入方法名</param>
  8.         ///<param name="args">传入参数,本参数为顺序参数</param>
  9.         public object InvokeWebservice(string url, string @namespacestring classname, string methodname, object[] args)
  10.         {
  11.             try
  12.             {
  13.                 System.Net.WebClient wc = new System.Net.WebClient();
  14.                 System.IO.Stream stream = wc.OpenRead(url + "?WSDL");
  15.                 System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.ServiceDescription.Read(stream);
  16.                 System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter();
  17.                 sdi.AddServiceDescription(sd, """");
  18.                 System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(@namespace);
  19.                 System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();
  20.                 ccu.Namespaces.Add(cn);
  21.                 sdi.Import(cn, ccu);
  22.                 Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider();
  23.                 System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler();
  24.                 System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.CompilerParameters();
  25.                 cplist.GenerateExecutable = false;
  26.                 cplist.GenerateInMemory = true;
  27.                 cplist.ReferencedAssemblies.Add("System.dll");
  28.                 cplist.ReferencedAssemblies.Add("System.XML.dll");
  29.                 cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
  30.                 cplist.ReferencedAssemblies.Add("System.Data.dll");
  31.                 System.CodeDom.Compiler.CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
  32.                 if (true == cr.Errors.HasErrors)
  33.                 {
  34.                     System.Text.StringBuilder sb = new System.Text.StringBuilder();
  35.                     foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
  36.                     {
  37.                         sb.Append(ce.ToString());
  38.                         sb.Append(System.Environment.NewLine);
  39.                     }
  40.                     throw new Exception(sb.ToString());
  41.                 }
  42.                 System.Reflection.Assembly assembly = cr.CompiledAssembly;
  43.                 Type t = assembly.GetType(@namespace + "." + classname, truetrue);
  44.                 object obj = Activator.CreateInstance(t);
  45.                 System.Reflection.MethodInfo mi = t.GetMethod(methodname);
  46.                 return mi.Invoke(obj, args);
  47.             }
  48.             catch (Exception ex)
  49.             {
  50.                 throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
  51.             }
  52.         }