1
using System;
2
using System.Net;
3
using System.IO;
4
using System.CodeDom;
5
using Microsoft.CSharp;
6
using System.CodeDom.Compiler;
7
using System.Web.Services.Description;
8
using System.Web.Services.Protocols;
9![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
namespace HB.Common
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* 调用方式
13
* string url = "http://www.webservicex.net/globalweather.asmx" ;
14
* string[] args = new string[2] ;
15
* args[0] = "Hangzhou";
16
* args[1] = "China" ;
17
* object result = WebServiceHelper.InvokeWebService(url ,"GetWeather" ,args) ;
18
* Response.Write(result.ToString());
19
*/
20
public class WebServiceHelper
21![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
InvokeWebService#region InvokeWebService
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
24
/// 动态调用web服务
25
/// </summary>
26
/// <param name="url">WSDL服务地址</param>
27
/// <param name="methodname">方法名</param>
28
/// <param name="args">参数</param>
29
/// <returns></returns>
30
public static object InvokeWebService(string url, string methodname, object[] args)
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
return WebServiceHelper.InvokeWebService(url, null, methodname, args);
33
}
34![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
36
/// 动态调用web服务
37
/// </summary>
38
/// <param name="url">WSDL服务地址</param>
39
/// <param name="classname">类名</param>
40
/// <param name="methodname">方法名</param>
41
/// <param name="args">参数</param>
42
/// <returns></returns>
43
public static object InvokeWebService(string url, string classname, string methodname, object[] args)
44![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
45
string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";
46
if ((classname == null) || (classname == ""))
47![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
48
classname = WebServiceHelper.GetWsClassName(url);
49
}
50![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
try
52![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53
//获取WSDL
54
WebClient wc = new WebClient();
55
Stream stream = wc.OpenRead(url + "?WSDL");
56
ServiceDescription sd = ServiceDescription.Read(stream);
57
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
58
sdi.AddServiceDescription(sd, "", "");
59
CodeNamespace cn = new CodeNamespace(@namespace);
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
//生成客户端代理类代码
62
CodeCompileUnit ccu = new CodeCompileUnit();
63
ccu.Namespaces.Add(cn);
64
sdi.Import(cn, ccu);
65
CSharpCodeProvider icc = new CSharpCodeProvider();
66![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
//设定编译参数
68
CompilerParameters cplist = new CompilerParameters();
69
cplist.GenerateExecutable = false;
70
cplist.GenerateInMemory = true;
71
cplist.ReferencedAssemblies.Add("System.dll");
72
cplist.ReferencedAssemblies.Add("System.XML.dll");
73
cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
74
cplist.ReferencedAssemblies.Add("System.Data.dll");
75![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
//编译代理类
77
CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
78
if (true == cr.Errors.HasErrors)
79![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
80
System.Text.StringBuilder sb = new System.Text.StringBuilder();
81
foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
82![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
83
sb.Append(ce.ToString());
84
sb.Append(System.Environment.NewLine);
85
}
86
throw new Exception(sb.ToString());
87
}
88![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89
//生成代理实例,并调用方法
90
System.Reflection.Assembly assembly = cr.CompiledAssembly;
91
Type t = assembly.GetType(@namespace + "." + classname, true, true);
92
object obj = Activator.CreateInstance(t);
93
System.Reflection.MethodInfo mi = t.GetMethod(methodname);
94![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
95
return mi.Invoke(obj, args);
96![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
97![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
98
PropertyInfo propertyInfo = type.GetProperty(propertyname);
99
return propertyInfo.GetValue(obj, null);
100
*/
101
}
102
catch (Exception ex)
103![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
104
throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
105
}
106
}
107![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
108
private static string GetWsClassName(string wsUrl)
109![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
110
string[] parts = wsUrl.Split('/');
111
string[] pps = parts[parts.Length - 1].Split('.');
112![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
113
return pps[0];
114
}
115
#endregion
116
}
117
}
118![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122
做人要厚道,转载请注明出处!