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:/temp http://localhost:1998/Implement/AgriProductService.svc /ser:DataContractSerializer
/tcv:Version35 /ct:System.Collections.ObjectModel.ObservableCollection`1 /reference:C:/"Program
Files"/"Reference Assemblies"/Microsoft/Framework/.NETFramework/v4.0/WindowsBase.dll
生成同步,帶事件的異步代碼,集合使用System.Collections.Generic.List集合
svcutil /a /d:d:/temp http://localhost:1998/Implement/AgriProductService.svc /ser:DataContractSerializer
/tcv:Version35 /ct:System.Collections.Generic.List`1
生成同步,帶事件的異步代碼,集合映射為數組
svcutil /a /d:d:/temp http://localhost:1998/Implement/AgriProductService.svc /ser:DataContractSerializer
/tcv:Version35
關於svcutil.exe的詳細介紹。
ServiceModel 元數據實用工具 (Svcutil.exe)
如果生成的代理類是要給silverlight用,還需要手動修改一下。因為silverlight不支持同步操作,所有需要刪除代理類中的同步操作的代碼,只保留異步的代碼,還有就是需要添加下面的代碼,用於open和close客戶端的代碼。
要是有只生成異步代碼的參數就更好了,好像目前還沒有發現,默認生成同步代碼,加上/async參數就同時生成異步代碼。
下面的代碼添加到client類中,public partial class Service1Client : System.ServiceModel.ClientBase<IService1>, IService1,這個類中。
private BeginOperationDelegate onBeginOpenDelegate;
private EndOperationDelegate onEndOpenDelegate;
private System.Threading.SendOrPostCallback onOpenCompletedDelegate;
private BeginOperationDelegate onBeginCloseDelegate;
private EndOperationDelegate onEndCloseDelegate;
private System.Threading.SendOrPostCallback onCloseCompletedDelegate;
public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> OpenCompleted;
public event System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs> CloseCompleted;
private System.IAsyncResult OnBeginOpen(object[] inValues, System.AsyncCallback callback, object asyncState)
{
return ((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(callback, asyncState);
}
private object[] OnEndOpen(System.IAsyncResult result)
{
((System.ServiceModel.ICommunicationObject)(this)).EndOpen(result); return null;
}
private void OnOpenCompleted(object state)
{
if ((this.OpenCompleted != null))
{
InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
this.OpenCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
}
}
public void OpenAsync()
{
this.OpenAsync(null);
}
public void OpenAsync(object userState)
{
if ((this.onBeginOpenDelegate == null))
{
this.onBeginOpenDelegate = new BeginOperationDelegate(this.OnBeginOpen);
}
if ((this.onEndOpenDelegate == null))
{
this.onEndOpenDelegate = new EndOperationDelegate(this.OnEndOpen);
}
if ((this.onOpenCompletedDelegate == null))
{
this.onOpenCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnOpenCompleted);
}
base.InvokeAsync(this.onBeginOpenDelegate, null, this.onEndOpenDelegate, this.onOpenCompletedDelegate, userState);
}
private System.IAsyncResult OnBeginClose(object[] inValues, System.AsyncCallback callback, object asyncState)
{
return ((System.ServiceModel.ICommunicationObject)(this)).BeginClose(callback, asyncState);
}
private object[] OnEndClose(System.IAsyncResult result)
{
((System.ServiceModel.ICommunicationObject)(this)).EndClose(result); return null;
}
private void OnCloseCompleted(object state)
{
if ((this.CloseCompleted != null))
{
InvokeAsyncCompletedEventArgs e = ((InvokeAsyncCompletedEventArgs)(state));
this.CloseCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
}
}
public void CloseAsync()
{
this.CloseAsync(null);
}
public void CloseAsync(object userState)
{
if ((this.onBeginCloseDelegate == null))
{
this.onBeginCloseDelegate = new BeginOperationDelegate(this.OnBeginClose);
}
if ((this.onEndCloseDelegate == null))
{
this.onEndCloseDelegate = new EndOperationDelegate(this.OnEndClose);
}
if ((this.onCloseCompletedDelegate == null))
{
this.onCloseCompletedDelegate = new System.Threading.SendOrPostCallback(this.OnCloseCompleted);
}
base.InvokeAsync(this.onBeginCloseDelegate, null, this.onEndCloseDelegate, this.onCloseCompletedDelegate, userState);
}
還要提醒大家的是,用svcutil生成的異步訪問代碼,直接給silverlight使用,還有有點問題的。和使用VS2010添加服務生成的代碼相比,有一些出入,比如沒有實現ChannelBase,沒有在Client中override CreateChannel方法,還有一些局部的差異,導致使用起來會有一些不同。
出現上面的問題,不知道是我沒有用對svcutil的參數,還是它們本身就是有一些不同的。
本文出自 “突破中的IT結構師” 博客,請務必保留此出處http://virusswb.blog.51cto.com/115214/891014