1/// <summary>
2/// 根据指定的信息,调用远程WebService方法
3/// </summary>
4/// <param name="url">WebService和Htt形式的地址</param>
5/// <param name="namespace">欲调用的WebService的命名空间</param>
6/// <param name="classname">欲调用的WebService的类名(不包括命名空间前缀)</param>
7/// <param name="methodname">欲调用的WebService的方法名</param>
8/// <param name="args">参数列表</param>
9/// <returns>WebService的执行结果</returns>
10/// <remarks>
11/// 如果调用失败,将会抛出Exception。请调用的时候,适当截获异常。
12/// 异常信息可能会发生在两个地方:
13/// 1、动态构造WebService的时候,CompileAssembly失败。
14/// 2、WebService本身执行失败。
15/// </remarks>
16/// <example>
17/// <code>
18/// object obj = InvokeWebService("http:localhost/GSP_WorkflowWebservice/common.asmx", "Genersoft.platform.Service.Workflow", "common", "GetToolType", new object[]{"1"});
19/// </code>
20/// </example>
21private object InvokeWebservice(string url, string @namespace, string classname, string methodname, object[] args)
22{
23 try
24 {
25 System.Net.WebClient wc = new System.Net.WebClient();
26 System.IO.Stream stream = wc.OpenRead(url + "?WSDL");
27 System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.ServiceDescription.Read(stream);
28 System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter();
29 sdi.AddServiceDescription(sd, "", "");
30 System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(@namespace);
31 System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();
32 ccu.Namespaces.Add(cn);
33 sdi.Import(cn, ccu);
34
35 Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider();
36 System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler();
37 System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.CompilerParameters();
38 cplist.GenerateExecutable = false;
39 cplist.GenerateInMemory = true;
40 cplist.ReferencedAssemblies.Add("System.dll");
41 cplist.ReferencedAssemblies.Add("System.XML.dll");
42 cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
43 cplist.ReferencedAssemblies.Add("System.Data.dll");
44
45 System.CodeDom.Compiler.CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
46 if (true == cr.Errors.HasErrors)
47 {
48 System.Text.StringBuilder sb = new StringBuilder();
49 foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
50 {
51 sb.Append(ce.ToString());
52 sb.Append(System.Environment.NewLine);
53 }
54 throw new Exception(sb.ToString());
55 }
56
57 System.Reflection.Assembly assembly = cr.CompiledAssembly;
58 Type t = assembly.GetType(@namespace + "." + classname, true, true);
59 object obj = Activator.CreateInstance(t);
60 System.Reflection.MethodInfo mi = t.GetMethod(methodname);
61 return mi.Invoke(obj, args);
62 }
63 catch(Exception ex)
64 {
65 throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
66 }
67}
2/// 根据指定的信息,调用远程WebService方法
3/// </summary>
4/// <param name="url">WebService和Htt形式的地址</param>
5/// <param name="namespace">欲调用的WebService的命名空间</param>
6/// <param name="classname">欲调用的WebService的类名(不包括命名空间前缀)</param>
7/// <param name="methodname">欲调用的WebService的方法名</param>
8/// <param name="args">参数列表</param>
9/// <returns>WebService的执行结果</returns>
10/// <remarks>
11/// 如果调用失败,将会抛出Exception。请调用的时候,适当截获异常。
12/// 异常信息可能会发生在两个地方:
13/// 1、动态构造WebService的时候,CompileAssembly失败。
14/// 2、WebService本身执行失败。
15/// </remarks>
16/// <example>
17/// <code>
18/// object obj = InvokeWebService("http:localhost/GSP_WorkflowWebservice/common.asmx", "Genersoft.platform.Service.Workflow", "common", "GetToolType", new object[]{"1"});
19/// </code>
20/// </example>
21private object InvokeWebservice(string url, string @namespace, string classname, string methodname, object[] args)
22{
23 try
24 {
25 System.Net.WebClient wc = new System.Net.WebClient();
26 System.IO.Stream stream = wc.OpenRead(url + "?WSDL");
27 System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.ServiceDescription.Read(stream);
28 System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter();
29 sdi.AddServiceDescription(sd, "", "");
30 System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(@namespace);
31 System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();
32 ccu.Namespaces.Add(cn);
33 sdi.Import(cn, ccu);
34
35 Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider();
36 System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler();
37 System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.CompilerParameters();
38 cplist.GenerateExecutable = false;
39 cplist.GenerateInMemory = true;
40 cplist.ReferencedAssemblies.Add("System.dll");
41 cplist.ReferencedAssemblies.Add("System.XML.dll");
42 cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
43 cplist.ReferencedAssemblies.Add("System.Data.dll");
44
45 System.CodeDom.Compiler.CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
46 if (true == cr.Errors.HasErrors)
47 {
48 System.Text.StringBuilder sb = new StringBuilder();
49 foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
50 {
51 sb.Append(ce.ToString());
52 sb.Append(System.Environment.NewLine);
53 }
54 throw new Exception(sb.ToString());
55 }
56
57 System.Reflection.Assembly assembly = cr.CompiledAssembly;
58 Type t = assembly.GetType(@namespace + "." + classname, true, true);
59 object obj = Activator.CreateInstance(t);
60 System.Reflection.MethodInfo mi = t.GetMethod(methodname);
61 return mi.Invoke(obj, args);
62 }
63 catch(Exception ex)
64 {
65 throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
66 }
67}