Svcutil.exe (ServiceModel Metadata Utility) 是 WCF 產生客戶端代理類別的核心命令列工具。掌握其關鍵參數,能精確控制同步/非同步方法、集合型別、目標 .NET 版本、資料繫結支援,並解決 Silverlight 專案的特殊相容性問題。
| 參數 | 縮寫 | 說明 | 預設值 |
|---|---|---|---|
/async | /a | 同時產生同步與非同步 (Begin/End + Event-based) 方法 | 僅同步 |
/tcv:Version35 | /tcv | 目標 .NET Framework 版本:Version30 / Version35 | Version30 |
/collectionType | /ct | 集合型別完整名稱 (如 ObservableCollection`1) | Array |
/reference | /r | 參考組件路徑,重用現有資料契約型別 | 無 |
/enableDataBinding | /edb | 所有資料契約實作 INotifyPropertyChanged | 不實作 |
/serializer | /ser | 序列化器:DataContractSerializer / XmlSerializer | DataContractSerializer |
/directory | /d | 輸出目錄 | 當前目錄 |
svcutil /a /d:d:/temp https://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"
svcutil /a /d:d:/temp https://localhost:1998/Implement/AgriProductService.svc
/ser:DataContractSerializer /tcv:Version35
/ct:System.Collections.Generic.List`1
svcutil /a /d:d:/temp https://localhost:1998/Implement/AgriProductService.svc
/ser:DataContractSerializer /tcv:Version35
Silverlight 不支援同步呼叫,且預設產生的代理缺少 ChannelBase 實作與 OpenAsync/CloseAsync。須手動調整:
保留 BeginXxx/EndXxx 與 XxxAsync/XxxCompleted 事件模式。
將以下代碼加入產生的 partial class ServiceClient : ClientBase<IService>, IService 中:
// 開啟連線非同步支援
private BeginOperationDelegate onBeginOpenDelegate;
private EndOperationDelegate onEndOpenDelegate;
private SendOrPostCallback onOpenCompletedDelegate;
public event EventHandler<AsyncCompletedEventArgs> OpenCompleted;
private IAsyncResult OnBeginOpen(object[] inValues, AsyncCallback callback, object asyncState)
{
return ((ICommunicationObject)this).BeginOpen(callback, asyncState);
}
private object[] OnEndOpen(IAsyncResult result)
{
((ICommunicationObject)this).EndOpen(result);
return null;
}
private void OnOpenCompleted(object state)
{
if (OpenCompleted != null)
{
var e = (InvokeAsyncCompletedEventArgs)state;
OpenCompleted(this, new AsyncCompletedEventArgs(e.Error, e.Cancelled, e.UserState));
}
}
public void OpenAsync() => OpenAsync(null);
public void OpenAsync(object userState)
{
onBeginOpenDelegate = onBeginOpenDelegate ?? new BeginOperationDelegate(OnBeginOpen);
onEndOpenDelegate = onEndOpenDelegate ?? new EndOperationDelegate(OnEndOpen);
onOpenCompletedDelegate = onOpenCompletedDelegate ?? new SendOrPostCallback(OnOpenCompleted);
base.InvokeAsync(onBeginOpenDelegate, null, onEndOpenDelegate, onOpenCompletedDelegate, userState);
}
// 關閉連線非同步支援 (同上模式,略)
VS 「加入服務參考」產生的代理已包含此邏輯;Svcutil 產生的版本可能缺失,需手動補齊或改用 VS 介面產生。
| 差異點 | Svcutil 產生 | VS Add Service Reference |
|---|---|---|
| ChannelBase 實作 | 可能缺失 | 完整 |
| CreateChannel Override | 可能缺失 | 有 |
| 非同步事件模式 | Event-based (SL 相容) | Task-based (NET 4.5+) + Event-based |
| 部分類別結構 | 單一檔案 | 多檔案 (Reference.cs + .xaml) |
Svcutil.exe 腳本化產生,確保版本一致、可控制參數。SlSvcUtil.exe (Silverlight 專用版本)。本文出自「突破中的IT架構師」博客:https://virusswb.blog.51cto.com/115214/891014