OperationContractAttribute 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指示方法定义一个操作,该操作是 Windows Communication Foundation (WCF) 应用程序中服务协定的一部分。
public ref class OperationContractAttribute sealed : Attribute[System.AttributeUsage(System.AttributeTargets.Method)]
public sealed class OperationContractAttribute : Attribute[<System.AttributeUsage(System.AttributeTargets.Method)>]
type OperationContractAttribute = class
    inherit AttributePublic NotInheritable Class OperationContractAttribute
Inherits Attribute- 继承
- 属性
示例
下面的代码示例演示只有一个操作的简单服务协定。
using System;
using System.Collections.Generic;
using System.Net.Security;
using System.ServiceModel;
using System.Text;
namespace Microsoft.WCF.Documentation
{
  [ServiceContract(Namespace="Microsoft.WCF.Documentation")]
  public interface ISampleService{
    // This operation specifies an explicit protection level requirement.
    [OperationContract(ProtectionLevel=ProtectionLevel.EncryptAndSign)]
    string SampleMethod(string msg);
  }
  class SampleService : ISampleService
  {
  #region ISampleService Members
  public string  SampleMethod(string msg)
  {
    Console.WriteLine("Called with: {0}", msg);
      return "The service greets you: " + msg;
  }
  #endregion
  }
}
Imports System.Net.Security
Imports System.ServiceModel
Imports System.Text
Namespace Microsoft.WCF.Documentation
  <ServiceContract(Namespace:="Microsoft.WCF.Documentation")> _
  Public Interface ISampleService
    ' This operation specifies an explicit protection level requirement.
    <OperationContract(ProtectionLevel:=ProtectionLevel.EncryptAndSign)> _
    Function SampleMethod(ByVal msg As String) As String
  End Interface
  Friend Class SampleService
      Implements ISampleService
  #Region "ISampleService Members"
  Public Function SampleMethod(ByVal msg As String) As String Implements ISampleService.SampleMethod
    Console.WriteLine("Called with: {0}", msg)
       Return "The service greets you: " & msg
  End Function
  #End Region
  End Class
End Namespace
下面的示例演示了一个服务,该服务实现指定三个操作的隐式服务协定。 其中两个操作是双向操作,即无论返回值是什么,它们都将基础响应消息返回至调用方。 第三个操作接收一个调用(基础入站消息),但不返回任何基础响应消息。
[ServiceContractAttribute]
public class OneAndTwoWay
{
  // The client waits until a response message appears.
  [OperationContractAttribute]
  public int MethodOne (int x, out int y)
  {
    y = 34;
    return 0;
  }
  // The client waits until an empty response message appears.
  [OperationContractAttribute]
  public void MethodTwo (int x)
  {
    return;
  }
  // The client returns as soon as an outbound message
  // is dispatched to the service; no response
  // message is generated or sent from the service.
  [OperationContractAttribute(IsOneWay=true)]
  public void MethodThree (int x)
  {
    return;
  }
}
注解
将 OperationContractAttribute 应用于方法,以指示该方法实现作为服务协定(由 ServiceContractAttribute 属性指定)一部分的服务操作。
使用 OperationContractAttribute 属性控制该操作的结构以及以元数据表示的值:
- Action 属性指定唯一标识该操作的操作。 WCF 根据其操作将请求消息调度到方法。 
- AsyncPattern 属性指示使用 Begin/End 方法对可以实现或异步调用该操作。 
- HasProtectionLevel 属性指示是否已显式设置 ProtectionLevel 属性。 
- IsOneWay 属性指示该操作只包含单个输入消息。 该操作没有关联的输出消息。 
- IsInitiating 属性指定该操作是否可以是会话中的初始操作。 
- 属性 IsTerminating 指定 WCF 是否在操作完成后尝试终止当前会话。 
- ProtectionLevel 属性指定运行时操作要求的消息级安全性。 
- ReplyAction 属性指定该操作答复消息的操作。 
OperationContractAttribute 属性声明方法是服务协定中的操作。 只有具有 OperationContractAttribute 属性的方法可作为服务操作公开。 不含有 OperationContractAttribute 标记的所有方法的服务协定不公开任何操作。
属性AsyncPattern指示 methodName> 和 End<methodName> 方法对Begin<构成了异步实现的单个操作, (客户端或服务) 。 异步实现操作的服务功能是服务实现详细信息,并且不会在元数据中(如 Web 服务描述语言 (WSDL))公开。
同样,客户端可选择异步调用与实现服务方法无关的操作。 如果服务方法需要花些时间才可将信息直接返回至客户端但又必须这样做时,建议在客户端异步调用服务操作。 有关详细信息,请参阅 AsyncPattern。
              IsOneWay 属性指示方法根本不返回任何值,包括空基础响应消息。 这种类型的方法对通知或事件样式通信十分有用。 但这种方法不能返回答复消息,因此该方法的声明必须返回 void。
重要
以编程方式检索此属性中的信息存储时,请使用 ContractDescription 类而不是反射。
注意
如果 IsOneWay 属性设置为 false(默认值),即使返回 void 的方法也是基础消息级别的双向方法。 在此种情况下,基础结构将创建并发送一条空消息,以向调用方指示该方法已返回内容。 使用此方法使应用程序和基础结构可以将错误信息(如 SOAP 错误)发送回客户端。 将 IsOneWay 设置为 true 是阻止创建和调度答复消息的唯一方法。 有关详细信息,请参阅单向服务。
Action 和 ReplyAction 属性不仅可用于修改 SOAP 消息的默认操作,也可用于为无法识别的消息创建处理程序,或为直接消息编程禁用添加操作。 使用 IsInitiating 属性以防止客户端在进行其他操作前调用特定的服务操作。 IsTerminating使用 属性让 WCF 在客户端调用特定服务操作后关闭通道。 有关详细信息,请参阅使用会话。
ProtectionLevel 属性使您可以在操作协定中指定该操作消息是否已签名和/或已加密。 如果绑定无法提供协定中要求的安全级别,则运行时将引发异常。 有关详细信息,请参阅 ProtectionLevel 和 了解保护级别。
构造函数
| OperationContractAttribute() | 初始化 OperationContractAttribute 类的新实例。 | 
属性
| Action | 获取或设置请求消息的 WS-Addressing 操作。 | 
| AsyncPattern | 指示操作是使用服务协定中的  | 
| HasProtectionLevel | 获取一个值,该值指示是否必须对此操作的消息进行加密和/或签名。 | 
| IsInitiating | 获取或设置一个值,该值指示方法是否实现可在服务器上启动会话(如果存在会话)的操作。 | 
| IsOneWay | 获取或设置一个值,该值指示操作是否返回答复消息。 | 
| IsTerminating | 获取或设置一个值,该值指示服务操作在发送答复消息(如果存在)后,是否会导致服务器关闭会话。 | 
| Name | 获取或设置操作的名称。 | 
| ProtectionLevel | 获取或设置一个值,该值指定是否必须对操作的消息进行加密和/或签名。 | 
| ReplyAction | 获取或设置用于该操作答复消息的 SOAP 操作的值。 | 
| TypeId | 在派生类中实现时,获取此 Attribute 的唯一标识符。(继承自 Attribute) | 
方法
| Equals(Object) | 返回一个值,该值指示此实例是否与指定的对象相等。(继承自 Attribute) | 
| GetHashCode() | 返回此实例的哈希代码。(继承自 Attribute) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| IsDefaultAttribute() | 在派生类中重写时,指示此实例的值是否是派生类的默认值。(继承自 Attribute) | 
| Match(Object) | 当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。(继承自 Attribute) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
显式接口实现
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) | 将一组名称映射为对应的一组调度标识符。(继承自 Attribute) | 
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) | 检索对象的类型信息,然后可以使用该信息获取接口的类型信息。(继承自 Attribute) | 
| _Attribute.GetTypeInfoCount(UInt32) | 检索对象提供的类型信息接口的数量(0 或 1)。(继承自 Attribute) | 
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | 提供对某一对象公开的属性和方法的访问。(继承自 Attribute) |