指定服务运行时行为

设计服务协定(设计服务协定)并实现服务协定(实现服务协定)后,可以配置服务运行时的作行为。 本主题讨论系统提供的服务和作行为,并介绍在何处查找创建新行为的详细信息。 虽然某些行为作为属性应用,但许多行为是使用应用程序配置文件或以编程方式应用的。 有关配置服务应用程序的详细信息,请参阅 “配置服务”。

概述

协定定义该类型的服务的输入、输出、数据类型和功能。 通过实施服务合同,创建一个类,该类在配置了地址绑定后,即满足所实现的合同。 客户知道合同、绑定及地址信息;没有这些信息,客户无法使用该服务。

但是,作细节(例如线程问题或实例管理)对客户端不透明。 一旦您实现了服务协定,便可以使用行为来配置大量操作特性。 行为是一些对象,可通过设置运行时属性或通过在运行时中插入自定义类型来修改 Windows Communication Foundation (WCF) 运行时。 有关通过创建用户定义的行为修改运行时的详细信息,请参阅 扩展 ServiceHost 和服务模型层

属性System.ServiceModel.ServiceBehaviorAttributeSystem.ServiceModel.OperationBehaviorAttribute是最广泛实用的行为,并揭示了最常请求的操作功能。 因为它们是属性,因此将它们应用于服务或作实现。 其他行为(如 System.ServiceModel.Description.ServiceMetadataBehaviorSystem.ServiceModel.Description.ServiceDebugBehavior)通常使用应用程序配置文件应用,尽管可以通过编程方式使用它们。

本主题概述了这些 ServiceBehaviorAttribute 属性和 OperationBehaviorAttribute 属性,介绍了各种行为可以运行的范围,并快速说明 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

许多属性需要绑定的额外支持。 例如,要求来自客户端的事务的操作必须配置为使用支持流事务的绑定。

已知的单一实例服务

可以使用 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.ConcurrencyModeConcurrencyMode.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。 指定绑定必须提供给服务或客户端实现的功能要求。

操作行为

以下操作行为指定操作的序列化和事务控制。

另请参阅