动态调用Web Service
代码
System.Net.WebClient wc = new System.Net.WebClient();
System.IO.Stream stream = wc.OpenRead("http://localhost:44763/Jquery/DynamicInvoke.asmx?WSDL");
//获取Web服务描述
System.Web.Services.Description.ServiceDescription sd =System.Web.Services.Description.ServiceDescription.Read(stream);
System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter();
sdi.AddServiceDescription(sd, "", "");
System.CodeDom.CodeNamespace ns = new System.CodeDom.CodeNamespace();
System.CodeDom.CodeCompileUnit cu = new System.CodeDom.CodeCompileUnit();
cu.Namespaces.Add(ns);
sdi.Import(ns, cu);//导入指定的web服务描述
//创建csharp代码提供程序
Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
//创建csharp代码编译器
System.CodeDom.Compiler.ICodeCompiler compiler = provider.CreateCompiler();
//调用参数设置
System.CodeDom.Compiler.CompilerParameters parms = new System.CodeDom.Compiler.CompilerParameters();
parms.GenerateExecutable = false;
parms.GenerateInMemory = true;
parms.ReferencedAssemblies.Add("System.dll");
parms.ReferencedAssemblies.Add("System.XML.dll");
parms.ReferencedAssemblies.Add("System.Web.Services.dll");
parms.ReferencedAssemblies.Add("System.Data.dll");
//生成程序集
System.CodeDom.Compiler.CompilerResults result = provider.CompileAssemblyFromDom(parms, cu);
if (true == result.Errors.HasErrors)
{
}
System.Reflection.Assembly assembly = result.CompiledAssembly;
Type t = assembly.GetType("DynamicInvoke", true, true);
object obj = Activator.CreateInstance(t);
System.Reflection.MethodInfo mi = t.GetMethod("HelloWorld");
Response.Write(mi.Invoke(obj, null));
System.IO.Stream stream = wc.OpenRead("http://localhost:44763/Jquery/DynamicInvoke.asmx?WSDL");
//获取Web服务描述
System.Web.Services.Description.ServiceDescription sd =System.Web.Services.Description.ServiceDescription.Read(stream);
System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter();
sdi.AddServiceDescription(sd, "", "");
System.CodeDom.CodeNamespace ns = new System.CodeDom.CodeNamespace();
System.CodeDom.CodeCompileUnit cu = new System.CodeDom.CodeCompileUnit();
cu.Namespaces.Add(ns);
sdi.Import(ns, cu);//导入指定的web服务描述
//创建csharp代码提供程序
Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
//创建csharp代码编译器
System.CodeDom.Compiler.ICodeCompiler compiler = provider.CreateCompiler();
//调用参数设置
System.CodeDom.Compiler.CompilerParameters parms = new System.CodeDom.Compiler.CompilerParameters();
parms.GenerateExecutable = false;
parms.GenerateInMemory = true;
parms.ReferencedAssemblies.Add("System.dll");
parms.ReferencedAssemblies.Add("System.XML.dll");
parms.ReferencedAssemblies.Add("System.Web.Services.dll");
parms.ReferencedAssemblies.Add("System.Data.dll");
//生成程序集
System.CodeDom.Compiler.CompilerResults result = provider.CompileAssemblyFromDom(parms, cu);
if (true == result.Errors.HasErrors)
{
}
System.Reflection.Assembly assembly = result.CompiledAssembly;
Type t = assembly.GetType("DynamicInvoke", true, true);
object obj = Activator.CreateInstance(t);
System.Reflection.MethodInfo mi = t.GetMethod("HelloWorld");
Response.Write(mi.Invoke(obj, null));