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

COM 呼叫 WCF 服務實戰 (二):MEX 契約動態取得中繼資料零部署調用

     編輯:關於.NET

系列第二篇介紹 **MEX (Metadata Exchange) 契約** 服務專用字串:透過服務端暴露的 MEX 端點動態下載中繼資料,建立通道調用 WCF。**無需產生 Proxy 組件、無需註冊 COM 類型、無需 GAC**,僅需服務端開啟 MEX 端點。適合客戶端環境未安裝 .NET Framework、或不想部署強型別組件的場景 (如 Excel 巨集分發至大量無 .NET 機器)。

核心優勢

  • **零部署**:客戶端只需腳本/VBA,無任何 .NET 組件
  • **動態中繼資料**:服務介面變更,客戶端無需重編譯/重部署即可生效
  • **標準化**:WS-MetadataExchange 規範,互操作性強

服務端:同第一篇,需確保 MEX 端點啟用

<service name="WCFServiceMoniker.Service1" ...>
  <endpoint address="" binding="wsHttpBinding" contract="WCFServiceMoniker.IService1" />
  <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
<behaviors>
  <serviceBehaviors>
    <behavior ...>
      <serviceMetadata httpGetEnabled="true" /> <!-- 也支援 ?wsdl -->
    </behavior>
  </serviceBehaviors>
</behaviors>

從 WSDL 解析專用字串必要參數

瀏覽 https://localhost/WCFServiceMoniker/Service1.svc?wsdl,關鍵資訊:

  • 服務位址:https://localhost/WCFServiceMoniker/Service1.svc
  • MEX 位址:https://localhost/WCFServiceMoniker/Service1.svc/mex
  • 繫結名稱:WSHttpBinding_IService1 (來自 <wsdl:binding name="WSHttpBinding_IService1">)
  • 繫結命名空間:https://tempuri.org/
  • 契約名稱:IService1 (來自 <wsdl:portType name="IService1">)
  • 契約命名空間:https://tempuri.org/

圖 1:WSDL 中提取的關鍵參數

VBScript 完整實作

' CallWCFService_mex.vbs
'---------------------------------------------------------------
' MEX service moniker example
'---------------------------------------------------------------

mexMonikerString = "service:mexAddress='https://localhost/WCFServiceMoniker/Service1.svc/mex'"
mexMonikerString = mexMonikerString + ", address='https://localhost/WCFServiceMoniker/Service1.svc'"
mexMonikerString = mexMonikerString + ", binding=WSHttpBinding_IService1, bindingNamespace='https://tempuri.org/'"
mexMonikerString = mexMonikerString + ", contract=IService1, contractNamespace='https://tempuri.org/'"

Set mexServiceMoniker = GetObject(mexMonikerString)
WScript.Echo mexServiceMoniker.SayHello("I am LazyBee, My blog is https://lazybee.cnblogs.com/")
Set mexServiceMoniker = Nothing

VBA (Word/Excel 巨集) 實作

Sub CallWCFService_Mex()
    Dim mexMonikerString As String
    Dim mexServiceMoniker As Object

    mexMonikerString = "service:mexAddress='https://localhost/WCFServiceMoniker/Service1.svc/mex'"
    mexMonikerString = mexMonikerString + ", address='https://localhost/WCFServiceMoniker/Service1.svc'"
    mexMonikerString = mexMonikerString + ", binding=WSHttpBinding_IService1, bindingNamespace='https://tempuri.org/'"
    mexMonikerString = mexMonikerString + ", contract=IService1, contractNamespace='https://tempuri.org/'"

    Set mexServiceMoniker = GetObject(mexMonikerString)
    MsgBox mexServiceMoniker.SayHello("I am LazyBee, My blog is https://lazybee.cnblogs.com/")
    Set mexServiceMoniker = Nothing
End Sub

VB6 代碼與 VBA 完全相同。

執行結果驗證

圖 2:VBScript 執行成功輸出

MEX 專用字串四大參數對照表

參數範例值來源備註
mexAddresshttps://.../Service1.svc/mex服務端 MEX 端點位址通常為服務位址 + /mex
addresshttps://.../Service1.svc實際通訊端點與 mexAddress 分離,支援負載平衡
binding / bindingNamespaceWSHttpBinding_IService1 / https://tempuri.org/WSDL binding 區塊**嚴格區分大小寫**
contract / contractNamespaceIService1 / https://tempuri.org/WSDL portType 區塊**嚴格區分大小寫**

限制與注意事項

  • 不支援設定檔繫結參數:繫結名稱寫死在專用字串,無法透過 bindingConfiguration 注入 maxReceivedMessageSize 等設定。大訊息場景必須改用類型化契約 (見第 4 篇)
  • 首次調用較慢:需下載並解析 MEX 中繼資料,後續調用快取較快
  • 服務端必須開啟 MEX:安全合規若禁用 MEX,不可用此模式 (改用 WSDL 契約,見第 3 篇)
  • 命名空間預設陷阱:未自訂命名空間時均為 https://tempuri.org/,生產環境建議明確指定

總結

MEX 契約在 **「無 .NET 客戶端環境」+「服務端可暴露 MEX」** 的雙重條件下,提供最便捷的零部署 WCF 調用體驗。VBScript/VBA/VB6 共用同一專用字串語法,極大降低傳統應用整合 WCF 的門檻。

下一篇將介紹 WSDL 契約:當 MEX 也被禁用時的備選方案。

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