svcutil.exe

参数
1 /async

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



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

缩写形式:/a



2 /tcv:Version35

/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:<类型>

/collectionType:<类型>


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

缩写形式:/ct



4/reference:<文件路径>

/reference:<文件路径>


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



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

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

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

缩写形式:/r

5/enableDataBinding

/enableDataBinding

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

缩写形式:/edb




示例:
1


生成同步,带事件的异步代码,集合使用System.Collections.ObjectModel.ObservableCollection集合,指定程序集引用

svcutil/a/d:d:/temphttp://localhost:1998/Implement/AgriProductService.svc/ser:DataContractSerializer/tcv:Version35/ct:System.Collections.ObjectModel.ObservableCollection`1/reference:C:/"ProgramFiles"/"ReferenceAssemblies"/Microsoft/Framework/.NETFramework/v4.0/WindowsBase.dll



2


生成同步,带事件的异步代码,集合使用System.Collections.Generic.List集合

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


3


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

svcutil/a/d:d:/temphttp://localhost:1998/Implement/AgriProductService.svc/ser:DataContractSerializer/tcv:Version35

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

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

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

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

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

privateBeginOperationDelegateonBeginOpenDelegate;privateEndOperationDelegateonEndOpenDelegate;privateSystem.Threading.SendOrPostCallbackonOpenCompletedDelegate;privateBeginOperationDelegateonBeginCloseDelegate;privateEndOperationDelegateonEndCloseDelegate;privateSystem.Threading.SendOrPostCallbackonCloseCompletedDelegate;publiceventSystem.EventHandler<System.ComponentModel.AsyncCompletedEventArgs>OpenCompleted;publiceventSystem.EventHandler<System.ComponentModel.AsyncCompletedEventArgs>CloseCompleted;privateSystem.IAsyncResultOnBeginOpen(object[]inValues,System.AsyncCallbackcallback,objectasyncState){return((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(callback,asyncState);}privateobject[]OnEndOpen(System.IAsyncResultresult){((System.ServiceModel.ICommunicationObject)(this)).EndOpen(result);returnnull;}privatevoidOnOpenCompleted(objectstate){if((this.OpenCompleted!=null)){InvokeAsyncCompletedEventArgse=((InvokeAsyncCompletedEventArgs)(state));this.OpenCompleted(this,newSystem.ComponentModel.AsyncCompletedEventArgs(e.Error,e.Cancelled,e.UserState));}}publicvoidOpenAsync(){this.OpenAsync(null);}publicvoidOpenAsync(objectuserState){if((this.onBeginOpenDelegate==null)){this.onBeginOpenDelegate=newBeginOperationDelegate(this.OnBeginOpen);}if((this.onEndOpenDelegate==null)){this.onEndOpenDelegate=newEndOperationDelegate(this.OnEndOpen);}if((this.onOpenCompletedDelegate==null)){this.onOpenCompletedDelegate=newSystem.Threading.SendOrPostCallback(this.OnOpenCompleted);}base.InvokeAsync(this.onBeginOpenDelegate,null,this.onEndOpenDelegate,this.onOpenCompletedDelegate,userState);}privateSystem.IAsyncResultOnBeginClose(object[]inValues,System.AsyncCallbackcallback,objectasyncState){return((System.ServiceModel.ICommunicationObject)(this)).BeginClose(callback,asyncState);}privateobject[]OnEndClose(System.IAsyncResultresult){((System.ServiceModel.ICommunicationObject)(this)).EndClose(result);returnnull;}privatevoidOnCloseCompleted(objectstate){if((this.CloseCompleted!=null)){InvokeAsyncCompletedEventArgse=((InvokeAsyncCompletedEventArgs)(state));this.CloseCompleted(this,newSystem.ComponentModel.AsyncCompletedEventArgs(e.Error,e.Cancelled,e.UserState));}}publicvoidCloseAsync(){this.CloseAsync(null);}publicvoidCloseAsync(objectuserState){if((this.onBeginCloseDelegate==null)){this.onBeginCloseDelegate=newBeginOperationDelegate(this.OnBeginClose);}if((this.onEndCloseDelegate==null)){this.onEndCloseDelegate=newEndOperationDelegate(this.OnEndClose);}if((this.onCloseCompletedDelegate==null)){this.onCloseCompletedDelegate=newSystem.Threading.SendOrPostCallback(this.OnCloseCompleted);}base.InvokeAsync(this.onBeginCloseDelegate,null,this.onEndCloseDelegate,this.onCloseCompletedDelegate,userState);}

还要提醒大家的是,用svcutil生成的异步访问代码,直接给silverlight使用,还有有点问题的。和使用VS2010添加服务生成的代码相比,有一些出入,比如没有实现ChannelBase,没有在Client中override CreateChannel方法,还有一些局部的差异,导致使用起来会有一些不同。

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