指定服務執行階段行為

一旦您設計好服務合約 (Designing Service Contracts) 並實作服務合約 (Implementing Service Contracts),就可以設定服務執行階段的作業行為。 本主題討論系統提供的服務與作業行為,並說明哪裡可以找到更多資訊以建立新行為。 儘管有些行為會以屬性形式來套用,許多行為還是需要透過應用程式組態檔或以程式設計方式來套用。 如需設定服務應用程式的詳細資訊,請參閱設定服務

概觀

合約會定義輸入、輸出、資料型別,以及該類型服務的功能。 實作服務合約會建立一個類別,此類別在使用位址上的繫結進行設定時會實行類別所實作的合約。 合約、繫結和位址資訊皆為用戶端所熟知,若缺乏這些資訊,用戶端就無法使用服務。

但是,用戶端並不清楚執行緒問題或執行個體管理之類的作業細節。 一旦您實作了服務合約,就可以透過「 行為」(Behavior) 來設定大量的作業特性。 行為是一種物件,可以藉由設定執行階段屬性或將自訂型別插入執行階段來修改 Windows Communication Foundation (WCF) 執行階段。 如需藉由建立使用者定義的行為來修改執行階段的詳細資訊,請參閱擴充 ServiceHost 與服務模型層

System.ServiceModel.ServiceBehaviorAttributeSystem.ServiceModel.OperationBehaviorAttribute 屬性都是常用的行為,且會公開最常要求的作業功能。 由於它們是屬性,您可以將它們套用至服務或作業實作中。 儘管您能以程式設計方式來使用諸如 System.ServiceModel.Description.ServiceMetadataBehaviorSystem.ServiceModel.Description.ServiceDebugBehavior的其他行為,一般來說您還是需要透過應用程式組態檔來加以套用。

本主題提供 ServiceBehaviorAttributeOperationBehaviorAttribute 屬性的概觀、說明行為可以進行作業的各種範圍,並針對 WCF 開發人員可能感興趣的許多系統提供行為提供各種範圍的簡要說明。

ServiceBehaviorAttribute 和 OperationBehaviorAttribute

最重要的行為是 ServiceBehaviorAttributeOperationBehaviorAttribute 屬性,可供您用來控制:

  • 執行個體存留期

  • 並行與同步化支援

  • 組態行為

  • 交易行為

  • 序列化行為

  • 中繼資料轉換

  • 工作階段存留期

  • 位址篩選與標頭處理

  • 模擬

  • 若要使用這些屬性,請以適用該範圍的屬性 (Attribute) 來標記服務或作業實作,然後設定屬性 (Property)。 例如,下列程式碼範例說明的作業實作會透過 OperationBehaviorAttribute.Impersonation 屬性要求此作業的呼叫端支援模擬。

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Threading;

namespace Microsoft.WCF.Documentation
{
  [ServiceContract(
    Name="SampleHello",
    Namespace="http://microsoft.wcf.documentation"
  )]
  public interface IHello
  {
    [OperationContract]
    string Hello(string greeting);
  }

  public class HelloService : IHello
  {

    public HelloService()
    {
      Console.WriteLine("Service object created: " + this.GetHashCode().ToString());
    }

    ~HelloService()
    {
      Console.WriteLine("Service object destroyed: " + this.GetHashCode().ToString());
    }

    [OperationBehavior(Impersonation=ImpersonationOption.Required)]
    public string Hello(string greeting)
    {
      Console.WriteLine("Called by: " + Thread.CurrentPrincipal.Identity.Name);
      Console.WriteLine("IsAuthenticated: " + Thread.CurrentPrincipal.Identity.IsAuthenticated.ToString());
      Console.WriteLine("AuthenticationType: " + Thread.CurrentPrincipal.Identity.AuthenticationType.ToString());

      Console.WriteLine("Caller sent: " + greeting);
      Console.WriteLine("Sending back: Hi, " + Thread.CurrentPrincipal.Identity.Name);
      return "Hi, " + Thread.CurrentPrincipal.Identity.Name;
    }
  }
}
Imports System.ServiceModel
Imports System.Threading

Namespace Microsoft.WCF.Documentation
    <ServiceContract(Name:="SampleHello", Namespace:="http://microsoft.wcf.documentation")> _
    Public Interface IHello
        <OperationContract> _
        Function Hello(ByVal greeting As String) As String
    End Interface

    Public Class HelloService
        Implements IHello

        Public Sub New()
            Console.WriteLine("Service object created: " & Me.GetHashCode().ToString())
        End Sub

        Protected Overrides Sub Finalize()
            Console.WriteLine("Service object destroyed: " & Me.GetHashCode().ToString())
        End Sub

        <OperationBehavior(Impersonation:=ImpersonationOption.Required)> _
        Public Function Hello(ByVal greeting As String) As String Implements IHello.Hello
            Console.WriteLine("Called by: " & Thread.CurrentPrincipal.Identity.Name)
            Console.WriteLine("IsAuthenticated: " & Thread.CurrentPrincipal.Identity.IsAuthenticated.ToString())
            Console.WriteLine("AuthenticationType: " & Thread.CurrentPrincipal.Identity.AuthenticationType.ToString())

            Console.WriteLine("Caller sent: " & greeting)
            Console.WriteLine("Sending back: Hi, " & Thread.CurrentPrincipal.Identity.Name)
            Return "Hi, " & Thread.CurrentPrincipal.Identity.Name
        End Function
    End Class
End Namespace

許多屬性都要求來自繫結的額外支援。 例如,要求來自用戶端之交易的作業必須設為使用可支援流動交易的繫結。

已知的單一服務

您可以使用 ServiceBehaviorAttributeOperationBehaviorAttribute 屬性控制特定存留期,存留期可以分別屬於實作作業的 InstanceContext 和服務物件。

例如, ServiceBehaviorAttribute.InstanceContextMode 屬性會控制多久釋放一次 InstanceContext ,而 OperationBehaviorAttribute.ReleaseInstanceModeServiceBehaviorAttribute.ReleaseServiceInstanceOnTransactionComplete 屬性則控制釋放服務物件的時機。

但是,您也可以自行建立服務物件,並透過該物件來建立服務主機。 若要這麼做,您必須同時將 ServiceBehaviorAttribute.InstanceContextMode 屬性設定為 Single ,否則在開啟服務主機時會擲回例外狀況。

請使用 ServiceHost(Object, Uri[]) 建構函式建立此類服務。 當您想要將特定物件執行個體提供給單一服務使用時,它提供了另一種實作自訂 System.ServiceModel.Dispatcher.IInstanceContextInitializer 的方式。 當您的服務實作型別難以建構時 (例如,無法實作沒有參數的預設公用建構函式時),您可以使用此多載。

請注意,提供物件給此建構函式時,某些與 Windows Communication Foundation (WCF) 執行個體行為相關的功能會以不同的方式運作。 例如,提供已知物件執行個體時,呼叫 InstanceContext.ReleaseServiceInstance 將沒有任何作用。 同樣的,也會忽略任何其他執行個體的釋放機制。 ServiceHost 類別的行為就像是所有作業的 OperationBehaviorAttribute.ReleaseInstanceMode 屬性都已設為 ReleaseInstanceMode.None

其他服務、端點、合約與作業行為

服務行為,例如 ServiceBehaviorAttribute 屬性,會在整個服務上作業。 例如,如果您將 ServiceBehaviorAttribute.ConcurrencyMode 屬性設為 ConcurrencyMode.Multiple ,就必須自行處理該服務中每項作業的執行緒同步化問題。 端點行為會在整個端點上作業;許多系統提供的端點行為都是因應用戶端功能而生。 合約行為會在合約層級作業,而作業行為則會修改作業遞送。

這些行為當中,有許多都是在屬性上實作的,而您能以使用 ServiceBehaviorAttributeOperationBehaviorAttribute 屬性的方式來使用它們,方法是將它們套用至適當的服務類別或作業實作上。 儘管您能以程式設計方式來使用諸如 ServiceMetadataBehaviorServiceDebugBehavior 物件的其他行為,一般來說您還是需要透過應用程式組態檔來加以套用。

例如,您可以透過 ServiceMetadataBehavior 物件來設定中繼資料的發行。 下列應用程式組態檔說明最常見的用法。

<configuration>
  <system.serviceModel>
    <services>
      <service 
        name="Microsoft.WCF.Documentation.SampleService"
        behaviorConfiguration="metadataSupport"
      >
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/SampleService" />
          </baseAddresses>
        </host>
        <endpoint
          address=""
          binding="wsHttpBinding"
          contract="Microsoft.WCF.Documentation.ISampleService"
        />
        <!-- Adds a WS-MetadataExchange endpoint at -->
        <!-- "http://localhost:8080/SampleService/mex" -->
        <endpoint
           address="mex"
           binding="mexHttpBinding"
           contract="IMetadataExchange"
        />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
      <behavior name="metadataSupport">
        <!-- Enables the IMetadataExchange endpoint in services that -->
        <!-- use "metadataSupport" in their behaviorConfiguration attribute. -->
        <!-- In addition, the httpGetEnabled and httpGetUrl attributes publish -->
        <!-- Service metadata for retrieval by HTTP/GET at the address -->
        <!-- "http://localhost:8080/SampleService?wsdl" -->
        <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  </system.serviceModel>
</configuration>

下列章節說明最好用的系統提供行為,供您用來修改服務或用戶端的執行階段遞送。 請參閱參考主題來判斷如何使用每一個項目。

服務行為

下列行為可在服務上作業。

端點行為

下列行為可在端點上作業。 這類行為大多可用於用戶端應用程式中。

合約行為

DeliveryRequirementsAttribute. 指定繫結必須提供給服務或用戶端實作的功能需求。

作業行為

下列作業行為指定作業的序列化與交易控制。

另請參閱