<serviceMetadata>

指定服務中繼資料和相關資訊的發行。

<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata>

Syntax

<serviceMetadata externalMetadataLocation="String"
                 httpGetBinding="String"
                 httpGetBindingConfiguration="String"
                 httpGetEnabled="Boolean"
                 httpGetUrl="String"
                 httpsGetBinding="String"
                 httpsGetBindingConfiguration="String"
                 httpsGetEnabled="Boolean"
                 httpsGetUrl="String"
                 policyVersion="Policy12/Policy15" />

屬性和項目

下列章節說明屬性、子元素和父元素。

屬性

屬性 描述
externalMetadataLocation 包含 WSDL 檔案位置的 URI,此位置會傳回給使用者,以回應 WSDL 和 MEX 要求,而非自動產生的 WSDL。 如果未設定這個屬性,則會傳回預設 WSDL。 預設為空字串。
httpGetBinding 字串,這個字串會指定透過 HTTP GET 擷取中繼資料所要使用之繫結的型別。 這個設定是選擇性的。 如果未指定這個設定,則會使用預設的繫結。

只有當繫結的內部繫結項目支援 IReplyChannel 時,這些繫結才會受到支援。 此外,該繫結的 MessageVersion 屬性 (Property) 必須是 None
httpGetBindingConfiguration 字串,這個字串會設定在 httpGetBinding 屬性中指定之繫結的名稱,該屬性會參考這個繫結中的其他組態資訊。 必須在 <bindings> 區段中定義相同的名稱。
httpGetEnabled 布林值,指定是否發行服務中繼資料以使用 HTTP/Get 要求進行擷取。 預設值為 false

如果未指定 httpGetUrl 屬性,則中繼資料的發行位址會是服務位址加上 "?wsdl"。 例如,如果服務位址為 http://localhost:8080/CalculatorService,則 HTTP/Get 中繼資料位址為 http://localhost:8080/CalculatorService?wsdl

如果此屬性為 false,或者服務的位址不是以 HTTP 或 HTTPS 為基礎,則會忽略 "?wsdl"。
httpGetUrl 布林值,指定中繼資料的發行位址以使用 HTTP/Get 要求進行擷取。 如果已指定相對 URI,則視為相對於服務的基底位址。
httpsGetBinding 字串,這個字串會指定透過 HTTPS GET 擷取中繼資料所要使用之繫結的型別。 這個設定是選擇性的。 如果未指定這個設定,則會使用預設的繫結。

只有當繫結的內部繫結項目支援 IReplyChannel 時,這些繫結才會受到支援。 此外,該繫結的 MessageVersion 屬性 (Property) 必須是 None
httpsGetBindingConfiguration 字串,這個字串會設定在 httpsGetBinding 屬性中指定之繫結的名稱,該屬性會參考這個繫結中的其他組態資訊。 必須在 <bindings> 區段中定義相同的名稱。
httpsGetEnabled 布林值,指定是否發行服務中繼資料以使用 HTTPS/Get 要求進行擷取。 預設值為 false

如果未指定 httpsGetUrl 屬性,則中繼資料的發行位址會是服務位址加上 "?wsdl"。 例如,如果服務位址為 https://localhost:8080/CalculatorService,則 HTTP/Get 中繼資料位址為 https://localhost:8080/CalculatorService?wsdl

如果此屬性為 false,或者服務的位址不是以 HTTP 或 HTTPS 為基礎,則會忽略 "?wsdl"。
httpsGetUrl URI,指定中繼資料的發行位址以使用 HTTPS/Get 要求進行擷取。
policyVersion 字串,指定所要使用的 WS-Policy 規格版本。 此屬性的型別為 PolicyVersion

子元素

父項目

元素 描述
<behavior> 指定行為項目。

備註

這個組態項目可讓您控制服務的中繼資料發行功能。 為了避免意外洩露潛在的敏感服務中繼資料,Windows Communication Foundation (WCF) 服務的預設設定會停用中繼資料發佈。 這個行為依預設為安全行為,但也表示您無法使用中繼資料匯入工具 (例如 Svcutil.exe) 來產生呼叫服務所需的用戶端程式碼,除非組態中已明確啟用服務的中繼發行行為。 使用這個組態項目,您就可以為服務啟用此發行行為。

如需設定此行為的詳細範例,請參閱中繼資料發佈行為

選用的 httpGetBindinghttpsGetBinding 屬性可讓您設定透過 HTTP GET (或 HTTPS GET) 擷取中繼資料時所使用的繫結。 如果未指定這些繫結,則會依適當情形,使用預設的繫結 (使用 HTTP 時為 HttpTransportBindingElement,使用 HTTPS 時則為 HttpsTransportBindingElement) 擷取中繼資料。 請注意,這些屬性 (Attribute) 無法搭配內建的 WCF 繫結使用。 只有當繫結的內部繫結項目支援 IReplyChannel 時,這些繫結才會受到支援。 此外,該繫結的 MessageVersion 屬性 (Property) 必須是 None

若要降低將服務暴露給惡意使用者的機會,可以使用 SSL over HTTP (HTTPS) 機制來進行安全的傳輸。 若要這麼做,您必須先將適當的 X.509 憑證,繫結至裝載服務之電腦上的特定連接埠 (如需詳細資訊,請參閱使用憑證)。其次,將此元素新增至服務設定,並將 httpsGetEnabled 屬性設定為 true。 最後,將 httpsGetUrl 屬性設定為服務中繼資料端點的 URL,如下列範例所示。

<behaviors>
  <serviceBehaviors>
    <behavior name="NewBehavior">
      <serviceMetadata httpsGetEnabled="true"
                       httpsGetUrl="https://myComputerName/myEndpoint" />
    </behavior>
  </serviceBehaviors>
</behaviors>

範例

下列範例使用 <serviceMetadata> 元素來設定服務,以公開中繼資料。 它也設定了一個端點,將 IMetadataExchange 合約公開做為 WS-MetadataExchange (MEX) 通訊協定的實作。 此範例使用 mexHttpBinding,這個方便的標準繫結,相當於將安全性模式設定為 wsHttpBindingNone。 相對位址 "mex" 會用於端點中,針對服務基底位址進行解析時,會產生端點位址 http://localhost/servicemodelsamples/service.svc/mex

<configuration>
  <system.serviceModel>
    <services>
      <service name="Microsoft.ServiceModel.Samples.CalculatorService"
               behaviorConfiguration="CalculatorServiceBehavior">
        <!-- This endpoint is exposed at the base address provided by the host: http://localhost/servicemodelsamples/service.svc -->
        <endpoint address=""
                  binding="wsHttpBinding"
                  contract="Microsoft.ServiceModel.Samples.ICalculator" />
        <!-- The mex endpoint is exposed at http://localhost/servicemodelsamples/service.svc/mex
             To expose the IMetadataExchange contract, you must enable the serviceMetadata behavior as demonstrated below. -->
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
    <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
    <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <!-- The serviceMetadata behavior publishes metadata through the IMetadataExchange contract. When this behavior is
               present, you can expose this contract through an endpoint as shown above. Setting httpGetEnabled to true publishes
               the service's WSDL at the <baseaddress>?wsdl eg. http://localhost/servicemodelsamples/service.svc?wsdl -->
          <serviceMetadata httpGetEnabled="True" />
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

另請參閱