共用方式為


定義服務 Run-Time 行為

設計服務合約(設計服務合約)並實作服務合約(實作服務合約)之後,您可以設定服務運行時間的作業行為。 本主題討論系統提供的服務和作業行為,並說明如何尋找詳細資訊以建立新行為。 雖然某些行為會套用為屬性,但許多行為會使用應用程式組態檔或以程式設計方式套用。 如需設定服務應用程式的詳細資訊,請參閱 設定服務

概觀

合約會定義該型別服務的輸入、輸出、數據類型和功能。 執行服務合約會建立一個類別,透過在特定位址上配置綁定,來履行其所實現的合約。 合約、具約束力和地址資訊都是用戶端所知;如果缺少這些信息,用戶端將無法使用服務。

不過,線程問題或實例管理等作業細節對客戶端來說是不透明的。 實作服務合約之後,您可以使用 行為來設定大量的作業特性。 行為是對 Windows Communication Foundation(WCF)運行時間進行修改的對象,這可以通過設定運行時間屬性或在運行時間中插入自定義類型來完成。 如需藉由建立使用者定義行為來修改運行時間的詳細資訊,請參閱 擴充 ServiceHost 和服務模型層

System.ServiceModel.ServiceBehaviorAttributeSystem.ServiceModel.OperationBehaviorAttribute 屬性是最廣泛的實用行為,並公開最常要求的作業功能。 因為它們是屬性,所以您會將它們套用至服務或作業實作。 其他行為,例如 System.ServiceModel.Description.ServiceMetadataBehaviorSystem.ServiceModel.Description.ServiceDebugBehavior,通常會使用應用程式組態檔來套用,不過您可以透過程序設計方式加以使用。

本主題提供 ServiceBehaviorAttributeOperationBehaviorAttribute 屬性的大綱描述,描述各種行為可作用的範圍,並簡述許多系統提供的行為,這些行為在各個範圍內可能對 WCF 開發人員具有吸引力。

ServiceBehaviorAttribute 和 OperationBehaviorAttribute

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

  • 實例生命周期

  • 並行和同步處理支援

  • 設定行為

  • 交易行為

  • 序列化行為

  • 元數據轉換

  • 會話存留期

  • 地址篩選和標頭處理

  • 模仿

  • 若要使用這些屬性,請使用適合該範圍的屬性來標記服務或作業實作,並設定屬性。 例如,下列程式代碼範例顯示使用 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

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

Well-Known 單例服務

您可以使用 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

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

服務行為,例如 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。 指定系結必須提供給服務或客戶端實作的功能需求。

作業行為

下列作業行為會指定作業的串行化和交易控件。

另請參閱