您是第欢迎光临我的主页位访客
浩凡儿
天行健,君子以自强不息;地势坤,君子以厚德载物!
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 using System.Web;
 7 using System.Net;
 8 using System.IO;
 9 using System.Web.Services.Description;
10 using System.CodeDom;
11 using Microsoft.CSharp;
12 using System.CodeDom.Compiler;
13 using System.Reflection;
14 
15 namespace GEB7.Core.Util
16 {
17     public class WSHelper
18     {
19 
20         ///    /// < summary>          
21         /// 动态调用web服务 
22         /// < /summary>          
23         /// < param name="url">WSDL服务地址< /param>
24         /// < param name="classname">类名< /param>  
25         /// < param name="methodname">方法名< /param>  
26         /// < param name="args">参数< /param> 
27         /// < returns>< /returns>
28         public static object InvokeWebService(string url, string classname, string methodname, object[] args)
29         {
30             string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";
31             if ((classname == null) || (classname == ""))
32             {
33                 classname = WSHelper.GetWsClassName(url);
34             }
35             try
36             {                   //获取WSDL   
37                 WebClient wc = new WebClient();
38                 Stream stream = wc.OpenRead(url + "?WSDL");
39                 ServiceDescription sd = ServiceDescription.Read(stream);
40                 ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
41                 sdi.AddServiceDescription(sd, "", "");
42                 CodeNamespace cn = new CodeNamespace(@namespace);
43                 //生成客户端代理类代码          
44                 CodeCompileUnit ccu = new CodeCompileUnit();
45                 ccu.Namespaces.Add(cn);
46                 sdi.Import(cn, ccu);
47                 CSharpCodeProvider icc = new CSharpCodeProvider();
48                 //设定编译参数                 
49                 CompilerParameters cplist = new CompilerParameters();
50                 cplist.GenerateExecutable = false;
51                 cplist.GenerateInMemory = true;
52                 cplist.ReferencedAssemblies.Add("System.dll");
53                 cplist.ReferencedAssemblies.Add("System.XML.dll");
54                 cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
55                 cplist.ReferencedAssemblies.Add("System.Data.dll");
56                 //编译代理类                 
57                 CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
58                 if (true == cr.Errors.HasErrors)
59                 {
60                     System.Text.StringBuilder sb = new System.Text.StringBuilder();
61                     foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
62                     {
63                         sb.Append(ce.ToString());
64                         sb.Append(System.Environment.NewLine);
65                     }
66                     throw new Exception(sb.ToString());
67                 }
68                 //生成代理实例,并调用方法   
69                 System.Reflection.Assembly assembly = cr.CompiledAssembly;
70                 Type t = assembly.GetType(@namespace + "." + classname, true, true);
71                 object obj = Activator.CreateInstance(t);
72                 System.Reflection.MethodInfo mi = t.GetMethod(methodname);
73                 return mi.Invoke(obj, args);
74             }
75             catch (Exception ex)
76             {
77                 throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
78             }
79         }
80         private static string GetWsClassName(string wsUrl)
81         {
82             string[] parts = wsUrl.Split('/');
83             string[] pps = parts[parts.Length - 1].Split('.');
84             return pps[0];
85         }
86     }
87 }

 

posted on 2018-04-10 15:57  浩凡儿  阅读(1980)  评论(0编辑  收藏  举报