C#动态调用webservice
法一: Web Service内容没有变,只是换了各地方。比如从localhost:8080/a.asmx换到了localhost:8090/a.asmx。如此一来你不必重新修改Web Reference只需要在使用WebMethod时
WebS.A a = new WebS.A(); a.Url="http://localhost:8090/a.asmx" a.remoteDosth();
可以把Url字符串存到config里面,用的时候去取。
方法二:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
object obj = InvokeWebservice("http://localhost/getPro.asmx","EtoonProduct.web.manage","getPro","getArea",new object[]{"1"});
this.DataGrid1.DataSource=obj;
this.DataGrid1.DataBind();
}
/// 根据指定的信息,调用远程WebService方法
/// WebService的http形式的地址
/// 欲调用的WebService的命名空间
/// 欲调用的WebService的类名(不包括命名空间前缀)
/// 欲调用的WebService的方法名
/// 参数列表
/// WebService的执行结果
/// 如果调用失败,将会抛出Exception。请调用的时候,适当截获异常。
/// 异常信息可能会发生在两个地方:
/// 1、动态构造WebService的时候,CompileAssembly失败。
/// 2、WebService本身执行失败。
/// object obj = InvokeWebservice("http://localhost/GSP_WorkflowWebservice/common.asmx","Genersoft.Platform.Service.Workflow","Common","GetToolType",new object[]{"1"});
private object InvokeWebservice(string url, string @namespace, string classname, string methodname, object[] args)
{
try
{
System.Net.WebClient wc = new System.Net.WebClient();
System.IO.Stream stream = wc.OpenRead(url+"?WSDL");
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 cn = new System.CodeDom.CodeNamespace(@namespace);
System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit();
ccu.Namespaces.Add(cn);
sdi.Import(cn,ccu);
Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler();
System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.CompilerParameters();
cplist.GenerateExecutable = false;
cplist.GenerateInMemory = true;
cplist.ReferencedAssemblies.Add("System.dll");
cplist.ReferencedAssemblies.Add("System.XML.dll");
cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
cplist.ReferencedAssemblies.Add("System.Data.dll");
System.CodeDom.Compiler.CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
if(true == cr.Errors.HasErrors)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach(System.CodeDom.Compiler.CompilerError ce in cr.Errors)
{
sb.Append(ce.ToString());
sb.Append(System.Environment.NewLine);
}
throw new Exception(sb.ToString());
}
System.Reflection.Assembly assembly = cr.CompiledAssembly;
Type t = assembly.GetType(@namespace+"."+classname,true,true);
object obj = Activator.CreateInstance(t);
System.Reflection.MethodInfo mi = t.GetMethod(methodname);
return mi.Invoke(obj,args);
}
catch(Exception ex)
{
throw new Exception(ex.InnerException.Message,new Exception(ex.InnerException.StackTrace));
}
}