C动态调用WebService服务方法

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

C#动态调用WebService服务方法调用举例:stringxmlTxt=WebServiceHelper.InvokeWebService(interFaceUrl,MethodName,Objpara).ToString();代码:usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.IO;usingSystem.Net;usingSystem.CodeDom;usingSystem.CodeDom.Compiler;usingSystem.Web.Services.Description;usingMicrosoft.CSharp;namespaceHDSS.Finance.Common{publicstaticclassWebServiceHelper{privatestaticDictionarystring,WebInvokePar_DicInvokeType;publicstaticDictionarystring,WebInvokeParDicInvokeType{get{if(_DicInvokeType==null)_DicInvokeType=newDictionarystring,WebInvokePar();return_DicInvokeType;}set{_DicInvokeType=value;}}///summary///动态调用WebService////summary///paramname=urlWebService地址/param///paramname=methodname方法名(模块名)/param///paramname=args参数列表,无参数为null/param///returnsobject/returnspublicstaticobjectInvokeWebService(stringurl,stringmethodname,paramsobject[]args){returnInvokeWebService(url,null,methodname,args);}publicstaticobjectInvokeWebService(stringurl,stringclassname,stringmethodname,paramsobject[]args){returnInvokeWebService(url,WebServer,classname,methodname,args);}///summary///动态调用WebService////summary///paramname=urlWebService地址/param///paramname=classname类名/param///paramname=methodname方法名(模块名)/param///paramname=args参数列表/param///returnsobject/returnspublicstaticobjectInvokeWebService(stringurl,string@namespace,stringclassname,stringmethodname,paramsobject[]args){url=url.Trim();WebInvokePartWebInkove=newWebInvokePar();System.Reflection.MethodInfomi;if(DicInvokeType.ContainsKey(url)){tWebInkove=DicInvokeType[url];if(tWebInkove!=null){mi=tWebInkove.WebType.GetMethod(methodname);//【11】if(mi!=null)returnmi.Invoke(tWebInkove.AssInstance,args);}}//string@namespace=fangqm.Netbank.WebService.webservice;if(classname==null||classname==){classname=WebServiceHelper.GetClassName(url);}//获取服务描述语言(WSDL)WebClientwc=newWebClient();Streamstream=wc.OpenRead(url+?WSDL);//【1】ServiceDescriptionsd=ServiceDescription.Read(stream);//【2】ServiceDescriptionImportersdi=newServiceDescriptionImporter();//【3】sdi.AddServiceDescription(sd,,);CodeNamespacecn=newCodeNamespace(@namespace);//【4】//生成客户端代理类代码CodeCompileUnitccu=newCodeCompileUnit();//【5】ccu.Namespaces.Add(cn);sdi.Import(cn,ccu);CSharpCodeProvidercsc=newCSharpCodeProvider();//【6】ICodeCompilericc=csc.CreateCompiler();//【7】//设定编译器的参数CompilerParameterscplist=newCompilerParameters();//【8】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);//编译代理类CompilerResultscr=icc.CompileAssemblyFromDom(cplist,ccu);//【9】if(true==cr.Errors.HasErrors){System.Text.StringBuildersb=newStringBuilder();foreach(CompilerErrorceincr.Errors){sb.Append(ce.ToString());sb.Append(System.Environment.NewLine);}thrownewException(sb.ToString());}//生成代理实例,并调用方法System.Reflection.Assemblyassembly=cr.CompiledAssembly;vartps=assembly.GetTypes();Typet=null;if(tps.Length==1)t=tps[0];elset=assembly.GetType(@namespace+.+classname,true,true);objectobj=Activator.CreateInstance(t);//【10】mi=t.GetMethod(methodname);//【11】if(mi==null)return;tWebInkove.AssInstance=obj;tWebInkove.WebType=t;tWebInkove.WebUrl=url;DicInvokeType[url]=tWebInkove;returnmi.Invoke(obj,args);}privatestaticstringGetClassName(stringurl){//假如URL为最终的返回值为Service1string[]parts=url.Split('/');string[]pps=parts[parts.Length-1].Split('.');returnpps[0];}publicclassWebInvokePar{publicstringWebUrl{get;set;}publicobjectAssInstance{get;set;}publicTypeWebType{get;set;}}}}

1 / 4
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功