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

简介:

 


svcutil.exe



参数


1 /async

 

 
  1. /async 同时生成同步和异步方法签名。 



默认设置:只生成同步方法签名。

缩写形式:/a



2 /tcv:Version35

 

 
  1. /tcv:Version35  


指定应用程序针对 .NET Framework 的哪个版本。有效值为:Version30 和 Version35。默认值为 Version30。

缩写形式:/tcv

Version30:如果为使用 .NET Framework 3.0 的客户端生成代码,则使用 /tcv:Version30。

Version35:如果为使用 .NET Framework 3.5 的客户端生成代码,则使用 /tcv:Version35。如果将 /tcv:Version35 

与 /async 开关一起使用,则会同时生成基于事件的异步方法和基于回调/委托的异步方法。



3/collectionType:<类型>

 

 
  1. /collectionType:<类型> 


从架构中生成代码时,指定要用作集合数据类型的完全限定或程序集限定名称。

缩写形式:/ct



4/reference:<文件路径>

 

 
  1. /reference:<文件路径> 


引用指定程序集中的类型。在生成客户端时,使用此选项来指定可能包含类型的程序集,这些类型表示所导入的元数据



无法使用此开关指定消息协定和 XmlSerializer 类型。

如果引用了 DateTimeOffset,则会使用此类型,而不是生成新类型。如果应用程序是使用 .NET Framework 3.5 编写

的,则 SvcUtil.exe 会自动引用 DateTimeOffset。

缩写形式:/r

5/enableDataBinding

 

 
  1. /enableDataBinding 

 

在所有数据协定类型上实现 INotifyPropertyChanged 接口以启用数据绑定。

缩写形式:/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


生成同步,带事件的异步代码,集合映射为数组

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

 

关于svcutil.exe的详细介绍,可以参看微软的MSDN。

ServiceModel 元数据实用工具 (Svcutil.exe)

 

如果生成的代理类是要给silverlight用,还需要手动修改一下。因为silverlight不支持同步操作,所有需要删除代理类中的同步操作的代码,只保留异步的代码,还有就是需要添加下面的代码,用于open和close客户端的代码。

要是有只生成异步代码的参数就更好了,好像目前还没有发现,默认生成同步代码,加上/async参数就同时生成异步代码。

 

下面的代码添加到client类中,public partial class Service1Client : System.ServiceModel.ClientBase<IService1>, IService1,这个类中。

 

 
  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使用,还有有点问题的。和使用VS2010添加服务生成的代码相比,有一些出入,比如没有实现ChannelBase,没有在Client中override CreateChannel方法,还有一些局部的差异,导致使用起来会有一些不同。

出现上面的问题,不知道是我没有用对svcutil的参数,还是它们本身就是有一些不同的。




本文转自 virusswb 51CTO博客,原文链接:http://blog.51cto.com/virusswb/891014,如需转载请自行联系原作者

目录
相关文章
|
6月前
|
Windows
WCF服务端调用客户端.
WCF服务端调用客户端.
|
数据格式 Windows JSON
微软 WCF的几种寄宿方式,寄宿IIS、寄宿winform、寄宿控制台、寄宿Windows服务
WCF寄宿方式是一种非常灵活的操作,可以在IIS服务、Windows服务、Winform程序、控制台程序中进行寄宿,从而实现WCF服务的运行,为调用者方便、高效提供服务调用。本文分别对这几种方式进行详细介绍并开发例子进行说明,以求大家对WCF寄宿的方式进行全面的认识和了解。
1004 0