资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

[WCF]使用Svcutil.exe生成客户端代理-创新互联


svcutil.exe



参数


1 /async

我们提供的服务有:成都网站建设、网站设计、微信公众号开发、网站优化、网站认证、金平ssl等。为上千余家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的金平网站制作公司
  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,这个类中。

  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 OpenCompleted; 
  14.  
  15. public event System.EventHandler 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的参数,还是它们本身就是有一些不同的。

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


文章题目:[WCF]使用Svcutil.exe生成客户端代理-创新互联
网站URL:http://cdkjz.cn/article/diccdp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220