![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
private void getSoapService(string WSDL) // vinent 050715 成功了!!! //并且不需要安装SOAP ToolKit 3.0 !!!
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
// <appSettings>
4
//<add key="WSDL" value="提供web服务说明的网址,例如:http://localhost/DynamicWebService2/WSReturnString.asmx?wsdl" />
5
// </appSettings>
6![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
//1)用XML阅读器从一个文件路径或者是网络路径中读入WSDL文件:
8
XmlTextReader reader = new XmlTextReader(System.Configuration.ConfigurationSettings.AppSettings[WSDL]);//加入 using System.Xml;
9
//2〉把读到的内容用ServiceDescription来描述:
10
ServiceDescription sd = ServiceDescription.Read(reader); // 加入 using System.Web.Services.Description;
11
//3)用服务导入器把得到的描述导入服务描述中:
12
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
13
sdi.AddServiceDescription(sd, null, null);
14
//4)指定要动态编译的代码的命名空间:
15
CodeNamespace cn = new CodeNamespace("DynamicServices");
16
//5)指定要动态编译的代码单元:
17
CodeCompileUnit ccu = new CodeCompileUnit();
18
//6)把命名空间添加到动态编译的代码单元的命名空间组中去:
19
ccu.Namespaces.Add(cn);
20
//7)指示服务导入器把该动态编译单元内该命名空间的代码导进去,作为新的动态服务描述:
21
sdi.Import(cn, ccu);
22
//8)新建一个 C#编译器来编译:
23
ICodeCompiler icc = new Microsoft.CSharp.CSharpCodeProvider().CreateCompiler(); //CSharpCodeProvider().CreateCompiler();
24
//9)新建一个编译参数对象(在这里我们设置它为空的编译参数):
25
CompilerParameters cp = new CompilerParameters(); // add by GUOGUO
26
//10)指示C#编译器,按照 CP 编译参数对象提供的参数来进行 CCU 单元的编译。并把编译结果传给 编译结果对象 cr:
27
CompilerResults cr = icc.CompileAssemblyFromDom(cp, ccu);
28
//11)从动态编译结果中获取 某个 字符串(通常是该类型名字)所表示的真实类 :
29
Type t = cr.CompiledAssembly.GetType("DynamicServices.WSReturnString"); // 其中的DynamicServices必须和CodeNamespace cn = new CodeNamespace("DynamicServices");定义的同名 //Type t = cr.CompiledAssembly.GetType("CodeDom.Service1"); //something wrong here
30
//12)创建该类的实例:
31
Object obj = Activator.CreateInstance(t);
32
//13)根据方法名称字符串 反射到该方法的实体:
33
System.Reflection.MethodInfo mi = obj.GetType().GetMethod("要调用的方法名称"); //MethodInfo mi = obj.GetType().GetMethod("Add");
34
//14)用该方法实体的.Invoke()来调用该方法:
35
//Label1.Text = System.Convert.ToString(mi.Invoke(obj, new object[] { "传给方法的参数1", "传给方法的参数2" })); //,new object[]{1,2})) ; //Console.WriteLine(System.Convert.ToString(mi.Invoke(obj, new object[]{1,2})));
36
}
37![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
38![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//**/
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
40
/// 根据指定的信息,调用远程WebService方法
41
/// </summary>
42
/// <param name="url">WebService的http形式的地址</param>
43
/// <param name="namespace">欲调用的WebService的命名空间</param>
44
/// <param name="classname">欲调用的WebService的类名(不包括命名空间前缀)</param>
45
/// <param name="methodname">欲调用的WebService的方法名</param>
46
/// <param name="args">参数列表</param>
47
/// <returns>WebService的执行结果</returns>
48
/// <remarks>
49
/// 如果调用失败,将会抛出Exception。请调用的时候,适当截获异常。
50
/// 异常信息可能会发生在两个地方:
51
/// 1、动态构造WebService的时候,CompileAssembly失败。
52
/// 2、WebService本身执行失败。
53
/// </remarks>
54
/// <example>
55
/// <code>
56
/// object obj = InvokeWebservice("http://localhost/GSP_WorkflowWebservice/common.asmx","Genersoft.Platform.Service.Workflow","Common","GetToolType",new object[]{"1"});
57
/// </code>
58
/// </example>
59
public static object InvokeWebservice(string url, string @namespace, string classname, string methodname, object[] args)
60![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
61
try
62![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
63
System.Net.WebClient wc = new System.Net.WebClient();
64
System.IO.Stream stream = wc.OpenRead(url + "?WSDL");
65
System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.ServiceDescription.Read(stream);
66
System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter();
67
sdi.AddServiceDescription(sd, "", "");
68
System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(@namespace);
69
System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();
70
ccu.Namespaces.Add(cn);
71
sdi.Import(cn, ccu);
72![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider();
74
System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler();
75![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.CompilerParameters();
77
cplist.GenerateExecutable = false;
78
cplist.GenerateInMemory = true;
79
cplist.ReferencedAssemblies.Add("System.dll");
80
cplist.ReferencedAssemblies.Add("System.XML.dll");
81
cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
82
cplist.ReferencedAssemblies.Add("System.Data.dll");
83![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
System.CodeDom.Compiler.CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
85
if (true == cr.Errors.HasErrors)
86![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
87
System.Text.StringBuilder sb = new System.Text.StringBuilder();
88
foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
89![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
90
sb.Append(ce.ToString());
91
sb.Append(System.Environment.NewLine);
92
}
93
throw new Exception(sb.ToString());
94
}
95
System.Reflection.Assembly assembly = cr.CompiledAssembly;
96
Type t = assembly.GetType(@namespace + "." + classname, true, true);
97
object obj = Activator.CreateInstance(t);
98
System.Reflection.MethodInfo mi = t.GetMethod(methodname);
99
return mi.Invoke(obj, args);
100
}
101
catch (Exception ex)
102![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
103
throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
104
}
105
}
网上找到的代码,还没整理。
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122
做人要厚道,转载请注明出处!