99偷拍视频精品区一区二,口述久久久久久久久久久久,国产精品夫妇激情啪发布,成人永久免费网站在线观看,国产精品高清免费在线,青青草在线观看视频观看,久久久久久国产一区,天天婷婷久久18禁,日韩动漫av在线播放直播

C#中怎么動態調用WebService

本篇文章為大家展示了C#中怎么動態調用WebService,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

成都創新互聯網絡公司擁有10余年的成都網站開發建設經驗,數千家客戶的共同信賴。提供做網站、網站制作、網站開發、網站定制、買友情鏈接、建網站、網站搭建、成都響應式網站建設公司、網頁設計師打造企業風格,提供周到的售前咨詢和貼心的售后服務

public static object InvokeWebService(string url,   string methodname, object[] args)

其中,url是Web服務的地址,methodname是要調用服務方法名,args是要調用Web服務所需的參數,返回值就是web服務返回的結果了。

要實現這樣的功能,你需要這幾個方面的技能:反射、CodeDom、編程使用C#編譯器、WebService。在了解這些知識后,就可以容易的實現web服務的動態調用了:

usingSystem.CodeDom.Compiler;  usingSystem;  usingSystem.Net;  usingSystem.CodeDom;  usingMicrosoft.CSharp;  usingSystem.IO;  usingSystem.Web.Services.Description;  usingSystem.Collections.Generic;  usingSystem.Reflection;  namespacecjl.WebServices  {  publicclassDynamicWebServices  {  staticSortedList〈string,Type〉_typeList=  newSortedList〈string,Type〉();   #regionInvokeWebService   staticstringGetCacheKey(stringurl,  stringclassName)  {  returnurl.ToLower()+className;  }  staticTypeGetTypeFromCache(stringurl,  stringclassName)  {  stringkey=GetCacheKey(url,className);  foreach(KeyValuePair〈string,Type〉  pairin_typeList)  {  if(key==pair.Key)  {  returnpair.Value;  }  }   returnnull;  }  staticTypeGetTypeFromWebService  (stringurl,stringclassName)  {  string@namespace="EnterpriseServerBase.  WebService.DynamicWebCalling";  if((className==null)||(className==""))  {  className=GetWsClassName(url);  }   //獲取WSDL  WebClientwc=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);  returnt;  }   //動態調用web服務  publicstaticobjectInvokeWebService  (stringurl,stringmethodName,object[]args)  {  returnInvokeWebService(url,null,  methodName,args);  }   publicstaticobjectInvokeWebService(stringurl,  stringclassName,stringmethodName,object[]args)  {  try  {  Typet=GetTypeFromCache(url,className);  if(t==null)  {  t=GetTypeFromWebService(url,className);   //添加到緩沖中  stringkey=GetCacheKey(url,className);  _typeList.Add(key,t);  }   objectobj=Activator.CreateInstance(t);  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  }  }

上面的注釋已經很好的說明了各代碼段的功能,下面給個例子看看,這個例子是通過訪問http://www.webservicex.net/globalweather.asmx服務來獲取各大城市的天氣狀況。

string url = "http://www.webservicex.  net/globalweather.asmx";  string[] args = new string[2];  args[0] = this.textBox_CityName.Text;  args[1] = "China";  object result = WebServiceHelper.  InvokeWebService(url, "GetWeather", args);  this.label_Result.Text = result.ToString();

上述內容就是C#中怎么動態調用WebService,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創新互聯行業資訊頻道。

分享名稱:C#中怎么動態調用WebService
文章源于:http://www.yijiale78.com/article46/gjheeg.html

成都網站建設公司_創新互聯,為您提供網站策劃網站導航域名注冊App設計企業建站微信公眾號

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都網頁設計公司