您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > WEBService的调用
动态调用WebService以及传递复杂对象1.动态调用WebService?解决方案:添加动态调用WebService类WebServiceHelper;代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Web;usingSystem.Net;usingSystem.Web.Services.Description;usingSystem.Text;usingSystem.IO;usingSystem.CodeDom;usingMicrosoft.CSharp;usingSystem.CodeDom.Compiler;///summary///WebServiceHelper的摘要说明////summarypublicclassWebServiceHelper{publicWebServiceHelper(){////TODO:在此处添加构造函数逻辑//}#regionInvokeWebService//动态调用web服务publicstaticobjectInvokeWebService(stringurl,stringmethodname,object[]args){returnWebServiceHelper.InvokeWebService(url,null,methodname,args);}publicstaticobjectInvokeWebService(stringurl,stringclassname,stringmethodname,object[]args){string@namespace=EnterpriseServerBase.WebService.DynamicWebCalling;if((classname==null)||(classname==)){classname=WebServiceHelper.GetWsClassName(url);}try{//获取WSDLWebClientwc=newWebClient();Streamstream=wc.OpenRead(url+?WSDL);ServiceDescriptionsd=ServiceDescription.Read(stream);ServiceDescriptionImportersdi=newServiceDescriptionImporter();sdi.AddServiceDescription(sd,,);CodeNamespacecn=newCodeNamespace(@namespace);//生成客户端代理类代码CodeCompileUnitccu=newCodeCompileUnit();ccu.Namespaces.Add(cn);sdi.Import(cn,ccu);CSharpCodeProvidercsc=newCSharpCodeProvider();ICodeCompilericc=csc.CreateCompiler();//设定编译参数CompilerParameterscplist=newCompilerParameters();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);if(true==cr.Errors.HasErrors){System.Text.StringBuildersb=newSystem.Text.StringBuilder();foreach(System.CodeDom.Compiler.CompilerErrorceincr.Errors){sb.Append(ce.ToString());sb.Append(System.Environment.NewLine);}thrownewException(sb.ToString());}//生成代理实例,并调用方法System.Reflection.Assemblyassembly=cr.CompiledAssembly;Typet=assembly.GetType(@namespace+.+classname,true,true);objectobj=Activator.CreateInstance(t);System.Reflection.MethodInfomi=t.GetMethod(methodname);returnmi.Invoke(obj,args);}catch(Exceptionex){thrownewException(ex.InnerException.Message,newException(ex.InnerException.StackTrace));}}privatestaticstringGetWsClassName(stringwsUrl){string[]parts=wsUrl.Split('/');string[]pps=parts[parts.Length-1].Split('.');returnpps[0];}#endregion}2.调用WebService时,传入参数为Entity[](即实体集合等复杂对象),如何传入以及接收参数?解决方案:第一步序列化对象#region序列化在内存中XmlSerializerxs=newXmlSerializer(typeof(Entity[]));MemoryStreammstream=newMemoryStream();XmlTextWriterwriter=newXmlTextWriter(mstream,Encoding.Default);xs.Serialize(mstream,en);writer.Close();stringstr=Encoding.UTF8.GetString(mstream.ToArray());#endregion#region写入文件再读取出字符串//FileStreamfs=newFileStream(c:\\aaa.txt,FileMode.OpenOrCreate);//xs=newXmlSerializer(typeof(Entity[]));//xs.Serialize(fs,en);//fs.Close();//fs=newFileStream(c:\\aaa.txt,FileMode.Open,FileAccess.Read);//StreamReaderreader=newStreamReader(fs,Encoding.Default);//str=reader.ReadToEnd();//reader.Close();//reader.Dispose();//fs.Close();//fs.Dispose();#endregion第二步:动态调用WebServicestringurl=[]args=newobject[2];args[0]=mapname;args[1]=str;objectresult=WebServiceHelper.InvokeWebService(url,AddEntity,args);第三步:WebService中的处理过程(反序列化)[WebMethod]publicboolAddEntity(stringmapname,stringstrentites){Streamstream=newMemoryStream(ASCIIEncoding.Default.GetBytes(strentites));XmlSerializerxml=newXmlSerializer(typeof(Entity[]));Entity[]entities=(Entity[])xml.Deserialize(stream);}
本文标题:WEBService的调用
链接地址:https://www.777doc.com/doc-2867070 .html