溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

[WCF]使用Svcutil.exe生成客戶端代理

發(fā)布時(shí)間:2020-06-22 03:04:05 來源:網(wǎng)絡(luò) 閱讀:1553 作者:virusswb 欄目:編程語言

 

 


svcutil.exe



參數(shù)


1 /async

 

  1. /async 同時(shí)生成同步和異步方法簽名。 



默認(rèn)設(shè)置:只生成同步方法簽名。

縮寫形式:/a



2 /tcv:Version35

 

  1. /tcv:Version35  


指定應(yīng)用程序針對 .NET Framework 的哪個(gè)版本。有效值為:Version30 和 Version35。默認(rèn)值為 Version30。

縮寫形式:/tcv

Version30:如果為使用 .NET Framework 3.0 的客戶端生成代碼,則使用 /tcv:Version30。

Version35:如果為使用 .NET Framework 3.5 的客戶端生成代碼,則使用 /tcv:Version35。如果將 /tcv:Version35

與 /async 開關(guān)一起使用,則會(huì)同時(shí)生成基于事件的異步方法和基于回調(diào)/委托的異步方法。



3/collectionType:<類型>

 

  1. /collectionType:<類型> 


從架構(gòu)中生成代碼時(shí),指定要用作集合數(shù)據(jù)類型的完全限定或程序集限定名稱。

縮寫形式:/ct



4/reference:<文件路徑>

 

  1. /reference:<文件路徑> 


引用指定程序集中的類型。在生成客戶端時(shí),使用此選項(xiàng)來指定可能包含類型的程序集,這些類型表示所導(dǎo)入的元數(shù)據(jù)



無法使用此開關(guān)指定消息協(xié)定和 XmlSerializer 類型。

如果引用了 DateTimeOffset,則會(huì)使用此類型,而不是生成新類型。如果應(yīng)用程序是使用 .NET Framework 3.5 編寫

的,則 SvcUtil.exe 會(huì)自動(dòng)引用 DateTimeOffset。

縮寫形式:/r

5/enableDataBinding

 

  1. /enableDataBinding 

 

在所有數(shù)據(jù)協(xié)定類型上實(shí)現(xiàn) INotifyPropertyChanged 接口以啟用數(shù)據(jù)綁定。

縮寫形式:/edb

 




示例:


1


生成同步,帶事件的異步代碼,集合使用System.Collections.ObjectModel.ObservableCollection集合,指定程序集引用

  1. svcutil /a /d:d:/temp http://localhost:1998/Implement/AgriProductService.svc /ser:DataContractSerializer  
  2.  
  3. /tcv:Version35 /ct:System.Collections.ObjectModel.ObservableCollection`1 /reference:C:/"Program  
  4.  
  5. Files"/"Reference Assemblies"/Microsoft/Framework/.NETFramework/v4.0/WindowsBase.dll 





2


生成同步,帶事件的異步代碼,集合使用System.Collections.Generic.List集合

  1. svcutil /a /d:d:/temp http://localhost:1998/Implement/AgriProductService.svc /ser:DataContractSerializer  
  2.  
  3. /tcv:Version35 /ct:System.Collections.Generic.List`1 




3


生成同步,帶事件的異步代碼,集合映射為數(shù)組

  1. svcutil /a /d:d:/temp http://localhost:1998/Implement/AgriProductService.svc /ser:DataContractSerializer  
  2.  
  3. /tcv:Version35 

 

關(guān)于svcutil.exe的詳細(xì)介紹,可以參看微軟的MSDN。

ServiceModel 元數(shù)據(jù)實(shí)用工具 (Svcutil.exe)

 

如果生成的代理類是要給silverlight用,還需要手動(dòng)修改一下。因?yàn)閟ilverlight不支持同步操作,所有需要?jiǎng)h除代理類中的同步操作的代碼,只保留異步的代碼,還有就是需要添加下面的代碼,用于open和close客戶端的代碼。

要是有只生成異步代碼的參數(shù)就更好了,好像目前還沒有發(fā)現(xiàn),默認(rèn)生成同步代碼,加上/async參數(shù)就同時(shí)生成異步代碼。

 

下面的代碼添加到client類中,public partial class Service1Client : System.ServiceModel.ClientBase<IService1>, IService1,這個(gè)類中。

 

  1. private BeginOperationDelegate onBeginOpenDelegate; 
  2.  
  3. private EndOperationDelegate onEndOpenDelegate; 
  4.  
  5. private System.Threading.SendOrPostCallback onOpenCompletedDelegate; 
  6.  
  7. private BeginOperationDelegate onBeginCloseDelegate; 
  8.  
  9. private EndOperationDelegate onEndCloseDelegate; 
  10.  
  11. private System.Threading.SendOrPostCallback onCloseCompletedDelegate; 
  12.  
  13. public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> OpenCompleted; 
  14.  
  15. public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> CloseCompleted; 
  16.  
  17.  
  18.  
  19.  
  20. private System.IAsyncResult OnBeginOpen(object[] inValues, System.AsyncCallback callback, object asyncState) 
  21.     return ((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(callback, asyncState); 
  22.  
  23. private object[] OnEndOpen(System.IAsyncResult result) 
  24.     ((System.ServiceModel.ICommunicationObject)(this)).EndOpen(result); return null; 
  25.  
  26. private void OnOpenCompleted(object state) 
  27.     if ((this.OpenCompleted != null)) 
  28.     { 
  29.         InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state)); 
  30.         this.OpenCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState)); 
  31.     } 
  32.  
  33.  
  34. public void OpenAsync() 
  35.     this.OpenAsync(null); 
  36.  
  37. public void OpenAsync(object userState) 
  38.     if ((this.onBeginOpenDelegate == null)) 
  39.     { 
  40.         this.onBeginOpenDelegate = new BeginOperationDelegate(this.OnBeginOpen); 
  41.     } 
  42.     if ((this.onEndOpenDelegate == null)) 
  43.     { 
  44.         this.onEndOpenDelegate = new EndOperationDelegate(this.OnEndOpen); 
  45.     } 
  46.     if ((this.onOpenCompletedDelegate == null)) 
  47.     { 
  48.         this.onOpenCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnOpenCompleted); 
  49.     } 
  50.     base.InvokeAsync(this.onBeginOpenDelegate, null, this.onEndOpenDelegate, this.onOpenCompletedDelegate, userState); 
  51.  
  52.  
  53. private System.IAsyncResult OnBeginClose(object[] inValues, System.AsyncCallback callback, object asyncState) 
  54.     return ((System.ServiceModel.ICommunicationObject)(this)).BeginClose(callback, asyncState); 
  55.  
  56. private object[] OnEndClose(System.IAsyncResult result) 
  57.     ((System.ServiceModel.ICommunicationObject)(this)).EndClose(result); return null; 
  58.  
  59. private void OnCloseCompleted(object state) 
  60.     if ((this.CloseCompleted != null)) 
  61.     { 
  62.         InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state)); 
  63.         this.CloseCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState)); 
  64.     } 
  65.  
  66. public void CloseAsync() 
  67.     this.CloseAsync(null); 
  68.  
  69. public void CloseAsync(object userState) 
  70.     if ((this.onBeginCloseDelegate == null)) 
  71.     { 
  72.         this.onBeginCloseDelegate = new BeginOperationDelegate(this.OnBeginClose); 
  73.     } 
  74.     if ((this.onEndCloseDelegate == null)) 
  75.     { 
  76.         this.onEndCloseDelegate = new EndOperationDelegate(this.OnEndClose); 
  77.     } 
  78.     if ((this.onCloseCompletedDelegate == null)) 
  79.     { 
  80.         this.onCloseCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnCloseCompleted); 
  81.     } 
  82.     base.InvokeAsync(this.onBeginCloseDelegate, null, this.onEndCloseDelegate, this.onCloseCompletedDelegate, userState); 

 

還要提醒大家的是,用svcutil生成的異步訪問代碼,直接給silverlight使用,還有有點(diǎn)問題的。和使用VS2010添加服務(wù)生成的代碼相比,有一些出入,比如沒有實(shí)現(xiàn)ChannelBase,沒有在Client中override CreateChannel方法,還有一些局部的差異,導(dǎo)致使用起來會(huì)有一些不同。

出現(xiàn)上面的問題,不知道是我沒有用對svcutil的參數(shù),還是它們本身就是有一些不同的。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI