首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

WCF 實戰:使用 Svcutil.exe 生成客戶端代理完整參數指南與 Silverlight 適配技巧

     編輯:關於.NET

前言

Svcutil.exe (ServiceModel Metadata Utility) 是 WCF 產生客戶端代理類別的核心命令列工具。掌握其關鍵參數,能精確控制同步/非同步方法、集合型別、目標 .NET 版本、資料繫結支援,並解決 Silverlight 專案的特殊相容性問題。

核心參數速查表

參數縮寫說明預設值
/async/a同時產生同步與非同步 (Begin/End + Event-based) 方法僅同步
/tcv:Version35/tcv目標 .NET Framework 版本:Version30 / Version35Version30
/collectionType/ct集合型別完整名稱 (如 ObservableCollection`1)Array
/reference/r參考組件路徑,重用現有資料契約型別無
/enableDataBinding/edb所有資料契約實作 INotifyPropertyChanged不實作
/serializer/ser序列化器:DataContractSerializer / XmlSerializerDataContractSerializer
/directory/d輸出目錄當前目錄

常用指令範例

1. 完整功能:非同步 + ObservableCollection + .NET 3.5 + 參考組件

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"

2. 非同步 + List<T> 集合

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

3. 僅非同步 + 陣列集合 (預設)

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

Silverlight 專案適配:三大必做修改

Silverlight 不支援同步呼叫,且預設產生的代理缺少 ChannelBase 實作與 OpenAsync/CloseAsync。須手動調整:

步驟 1:刪除所有同步方法

保留 BeginXxx/EndXxx 與 XxxAsync/XxxCompleted 事件模式。

步驟 2:補充 OpenAsync / CloseAsync 實作

將以下代碼加入產生的 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);
}

// 關閉連線非同步支援 (同上模式,略)

步驟 3:確保實作 ChannelBase 與 CreateChannel

VS 「加入服務參考」產生的代理已包含此邏輯;Svcutil 產生的版本可能缺失,需手動補齊或改用 VS 介面產生。

已知差異與避坑指南

差異點Svcutil 產生VS Add Service Reference
ChannelBase 實作可能缺失完整
CreateChannel Override可能缺失有
非同步事件模式Event-based (SL 相容)Task-based (NET 4.5+) + Event-based
部分類別結構單一檔案多檔案 (Reference.cs + .xaml)

建議工作流程

  1. 開發期:使用 VS 「加入服務參考」獲得完整設計時支援與 IntelliSense。
  2. 建置伺服器 / CI/CD:使用 Svcutil.exe 腳本化產生,確保版本一致、可控制參數。
  3. Silverlight 專案:VS 產生後手動刪除同步方法、補充 Open/Close Async,或直接使用 SlSvcUtil.exe (Silverlight 專用版本)。

本文出自「突破中的IT架構師」博客:https://virusswb.blog.51cto.com/115214/891014

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved