ServiceMetadataBehavior 是實現元資料發布的核心服務行為,支援 HTTP-GET 與 WS-MEX 兩種協定。本文介紹其屬性、組態對應 ServiceMetadataPublishingElement,以及 MEX 端點的特殊契約 IMetadataExchange 與專用繫結 MetadataExchangeBindings。
ExternalMetadataLocation:外部元資料位址。HttpGetEnabled/HttpGetUrl/HttpGetBinding:HTTP-GET 發布設定。HttpsGetEnabled/HttpsGetUrl/HttpsGetBinding:HTTPS-GET 發布設定。MetadataExporter:元資料導出器(預設 WsdlExporter)。對應組態元素 <serviceMetadata> 可設定上述屬性外,另提供 policyVersion 指定 WS-Policy 版本。
<behavior name="MetadataPublishBehavior">
<serviceMetadata externalMetadataLocation="https://127.0.1/mex/calculatorservice"
httpGetEnabled="true" httpGetUrl="https://127.0.0.1/calculatorservice/mex"
httpsGetEnabled="true" httpsGetUrl="https://127.0.0.1/calculatorservice/mex"
policyVersion="Policy15" />
</behavior>
定義於 System.ServiceModel.Description,僅含 Get(Message) 操作(同步與非同步),Action/ReplyAction 依 WS-Transfer Get 規範。ConfigurationName 設為 "IMetadataExchange",故組態可直接寫 contract="IMetadataExchange"。
靜態類別提供四種建立方法:CreateMexHttpBinding、CreateMexHttpsBinding、CreateMexTcpBinding、CreateMexNamedPipeBinding。組態對應 mexHttpBinding、mexHttpsBinding、mexTcpBinding、mexNamedPipeBinding。
<endpoint address="https://127.0.0.1/calculatorservice/mex" binding="mexHttpBinding" contract="IMetadataExchange" /> <endpoint address="https://127.0.0.1/calculatorservice/mex" binding="mexHttpsBinding" contract="IMetadataExchange" /> <endpoint address="net.tcp://127.0.0.1/calculatorservice/mex" binding="mexTcpBinding" contract="IMetadataExchange" /> <endpoint address="net.pipe://127.0.0.1/calculatorservice/mex" binding="mexNamedPipeBinding" contract="IMetadataExchange" />
下一篇將深入剖析 ServiceMetadataBehavior 內部如何實現兩種協定的發布邏輯。