共用方式為


<serviceMetadata>

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

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

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

屬性和項目

屬性

屬性 描述

externalMetadataLocation

包含 WSDL 檔案位置的 URI,此位置會傳回給使用者,以回應 WSDL 和 MEX 要求,而非自動產生的 WSDL。如果未設定這個屬性,則會傳回預設 WSDL。預設為空字串。

httpGetBinding

字串值,可使 HTTP GET 案例中所要使用的繫結依名稱指定。

httpGetEnabled

布林值,指定是否發行服務中繼資料以使用 HTTP/Get 要求進行擷取。預設為 false

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

如果這個屬性是 false,或服務的位址並非採用 HTTP 或 HTTPS,則會忽略 “?wsdl”。

httpGetUrl

布林值,指定中繼資料的發行位址以使用 HTTP/Get 要求進行擷取。

httpsGetBinding

字串值,可使 HTTPS GET 案例中所要使用的繫結依名稱指定。

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

子項目

父項目

項目 描述

<endpointBehaviors> 的 <behavior>

指定行為項目。

備註

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

如需設定此行為的詳細程式碼範例,請參閱Metadata Publishing Behavior

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

若要降低將服務暴露給惡意使用者的機會,可以使用 SSL over HTTP (HTTPS) 機制來進行安全的傳輸。若要這麼做,您必須先將適當的 X.509 憑證,繫結至裝載服務之電腦上的特定連接埠 (如需詳細資訊,請參閱 Working with Certificates)。其次,將這個項目加入至服務組態,並將 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,這個方便的標準繫結,相當於將安全性模式設定為 NonewsHttpBinding。"mex" 的相對位址是用於端點中,當解析服務基底位址時,會產生 https://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: https://localhost/servicemodelsamples/service.svc  -->
      <endpoint address=""
                binding="wsHttpBinding"
                contract="Microsoft.ServiceModel.Samples.ICalculator" />
      <!-- the mex endpoint is exposed at https://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. https://localhost/servicemodelsamples/service.svc?wsdl -->
        <serviceMetadata httpGetEnabled="True"/>
        <serviceDebug includeExceptionDetailInFaults="False" />
      </behavior>
    </serviceBehaviors>
  </behaviors>

</system.serviceModel>

</configuration>

請參閱

參考

ServiceMetadataPublishingElement
ServiceMetadataBehavior

其他資源

Security Behaviors in WCF
Metadata Publishing Behavior