OperationContractAttribute OperationContractAttribute OperationContractAttribute OperationContractAttribute Class

定义

指示方法定义一个操作,该操作是 Windows Communication Foundation (WCF) 应用程序中服务协定的一部分。Indicates that a method defines an operation that is part of a service contract in a Windows Communication Foundation (WCF) application.

public ref class OperationContractAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Method)]
public sealed class OperationContractAttribute : Attribute
type OperationContractAttribute = class
    inherit Attribute
Public NotInheritable Class OperationContractAttribute
Inherits Attribute
继承
OperationContractAttributeOperationContractAttributeOperationContractAttributeOperationContractAttribute
属性

示例

下面的代码示例演示只有一个操作的简单服务协定。The following code example shows a simple service contract with one operation.

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

下面的示例演示了一个服务,该服务实现指定三个操作的隐式服务协定。The following example is a service that implements an implicit service contract that specifies three operations. 其中两个操作是双向操作,即无论返回值是什么,它们都将基础响应消息返回至调用方。Two of the operations are two-way operations, which return underlying response messages to the caller no matter what the return value is. 第三个操作接收一个调用(基础入站消息),但不返回任何基础响应消息。The third operation receives a call, an underlying inbound message, but returns no underlying response message.

[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 属性指定)一部分的服务操作。Apply the OperationContractAttribute to a method to indicate that the method implements a service operation as part of a service contract (specified by a ServiceContractAttribute attribute).

使用 OperationContractAttribute 属性控制该操作的结构以及以元数据表示的值:Use the OperationContractAttribute properties to control the structure of the operation and the values expressed in metadata:

  • Action 属性指定唯一标识该操作的操作。The Action property specifies the action that uniquely identifies this operation. WCF 请求将消息调度给根据其操作的方法。WCF dispatches request messages to methods based on their action.

  • AsyncPattern 属性指示使用 Begin/End 方法对可以实现或异步调用该操作。The AsyncPattern property indicates that the operation is implemented or can be called asynchronously using a Begin/End method pair.

  • HasProtectionLevel 属性指示是否已显式设置 ProtectionLevel 属性。The HasProtectionLevel property indicates whether the ProtectionLevel property has been explicitly set.

  • IsOneWay 属性指示该操作只包含单个输入消息。The IsOneWay property indicates that the operation only consists of a single input message. 该操作没有关联的输出消息。The operation has no associated output message.

  • IsInitiating 属性指定该操作是否可以是会话中的初始操作。The IsInitiating property specifies whether this operation can be the initial operation in a session.

  • IsTerminating属性指定是否尝试终止当前会话完成操作后 WCF。The IsTerminating property specifies whether WCF attempts to terminate the current session after the operation completes.

  • ProtectionLevel 属性指定运行时操作要求的消息级安全性。The ProtectionLevel property specifies the message-level security that an operation requires at run time.

  • ReplyAction 属性指定该操作答复消息的操作。The ReplyAction property specifies the action of the reply message for the operation.

OperationContractAttribute 属性声明方法是服务协定中的操作。The OperationContractAttribute attribute declares that a method is an operation in a service contract. 只有具有 OperationContractAttribute 属性的方法可作为服务操作公开。Only methods attributed with the OperationContractAttribute are exposed as service operations. 不含有 OperationContractAttribute 标记的所有方法的服务协定不公开任何操作。A service contract without any methods marked with the OperationContractAttribute exposes no operations.

AsyncPattern属性指示一对Begin < methodName> 和End < methodName> 方法可形成一个异步实现操作 (无论是在客户端或服务)。The AsyncPattern property indicates that a pair of Begin<methodName> and End<methodName> methods form a single operation implemented asynchronously (whether on the client or the service). 异步实现操作的服务功能是服务实现详细信息,并且不会在元数据中(如 Web 服务描述语言 (WSDL))公开。The ability of a service to implement operations asynchronously is a service implementation detail and is not exposed in metadata (such as Web Services Description Language (WSDL)).

同样,客户端可选择异步调用与实现服务方法无关的操作。Similarly, clients can choose to invoke operations asynchronously independent of how the service method is implemented. 如果服务方法需要花些时间才可将信息直接返回至客户端但又必须这样做时,建议在客户端异步调用服务操作。Calling service operations asynchronously in the client is recommended when a service method takes some time but must return information directly to the client. 有关详细信息,请参阅 AsyncPatternFor details, see AsyncPattern.

IsOneWay 属性指示方法根本不返回任何值,包括空基础响应消息。The IsOneWay property indicates that a method does not return any value at all, including an empty underlying response message. 这种类型的方法对通知或事件样式通信十分有用。This type of method is useful for notifications or event-style communication. 但这种方法不能返回答复消息,因此该方法的声明必须返回 voidMethods of this kind cannot return a reply message so the method's declaration must return void.

重要

当以编程方式检索此属性中的信息存储区,使用ContractDescription类而不是反射。When programmatically retrieving the information store in this attribute, use the ContractDescription class instead of reflection.

备注

如果 IsOneWay 属性设置为 false(默认值),即使返回 void 的方法也是基础消息级别的双向方法。If the IsOneWay property is set to false, (the default), even methods that return void are two-way methods at the underlying message level. 在此种情况下,基础结构将创建并发送一条空消息,以向调用方指示该方法已返回内容。In this case, the infrastructure creates and sends an empty message to indicate to the caller that the method has returned. 使用此方法使应用程序和基础结构可以将错误信息(如 SOAP 错误)发送回客户端。Using this approach enables the application and the infrastructure to send error information (such as a SOAP fault) back to the client. IsOneWay 设置为 true 是阻止创建和调度答复消息的唯一方法。Setting IsOneWay to true is the only way to prevent the creation and dispatch of a reply message. 有关详细信息,请参阅单向服务For more information, see One-Way Services.

ActionReplyAction 属性不仅可用于修改 SOAP 消息的默认操作,也可用于为无法识别的消息创建处理程序,或为直接消息编程禁用添加操作。The Action and ReplyAction properties can be used not only to modify the default action of SOAP messages but also to create handlers for unrecognized messages or to disable adding actions for direct message programming. 使用 IsInitiating 属性以防止客户端在进行其他操作前调用特定的服务操作。Use the IsInitiating property to prevent clients from calling a particular service operation prior to other operations. 使用IsTerminating属性让 WCF 客户端调用特定服务操作后,关闭通道。Use the IsTerminating property to have WCF close the channel after clients call a particular service operation. 有关详细信息,请参阅使用会话的For more information, see Using Sessions.

ProtectionLevel 属性使您可以在操作协定中指定该操作消息是否已签名和/或已加密。The ProtectionLevel property enables you to specify on the operation contract whether the operation messages are signed, encrypted, or signed and encrypted. 如果绑定无法提供协定中要求的安全级别,则运行时将引发异常。If a binding cannot provide the security level required by the contract, an exception is thrown at run time. 有关详细信息,请参阅ProtectionLevel了解保护级别For more information, see ProtectionLevel and Understanding Protection Level.

构造函数

OperationContractAttribute() OperationContractAttribute() OperationContractAttribute() OperationContractAttribute()

初始化 OperationContractAttribute 类的新实例。Initializes a new instance of the OperationContractAttribute class.

属性

Action Action Action Action

获取或设置请求消息的 WS-Addressing 操作。Gets or sets the WS-Addressing action of the request message.

AsyncPattern AsyncPattern AsyncPattern AsyncPattern

指示操作是使用服务协定中的 Begin<methodName> 和 End<methodName> 方法对异步实现的。Indicates that an operation is implemented asynchronously using a Begin<methodName> and End<methodName> method pair in a service contract.

HasProtectionLevel HasProtectionLevel HasProtectionLevel HasProtectionLevel

获取一个值,该值指示是否必须对此操作的消息进行加密和/或签名。Gets a value that indicates whether the messages for this operation must be encrypted, signed, or both.

IsInitiating IsInitiating IsInitiating IsInitiating

获取或设置一个值,该值指示方法是否实现可在服务器上启动会话(如果存在会话)的操作。Gets or sets a value that indicates whether the method implements an operation that can initiate a session on the server (if such a session exists).

IsOneWay IsOneWay IsOneWay IsOneWay

获取或设置一个值,该值指示操作是否返回答复消息。Gets or sets a value that indicates whether an operation returns a reply message.

IsTerminating IsTerminating IsTerminating IsTerminating

获取或设置一个值,该值指示服务操作在发送答复消息(如果存在)后,是否会导致服务器关闭会话。Gets or sets a value that indicates whether the service operation causes the server to close the session after the reply message, if any, is sent.

Name Name Name Name

获取或设置操作的名称。Gets or sets the name of the operation.

ProtectionLevel ProtectionLevel ProtectionLevel ProtectionLevel

获取或设置一个值,该值指定是否必须对操作的消息进行加密和/或签名。Gets or sets a value that specifies whether the messages of an operation must be encrypted, signed, or both.

ReplyAction ReplyAction ReplyAction ReplyAction

获取或设置用于该操作答复消息的 SOAP 操作的值。Gets or sets the value of the SOAP action for the reply message of the operation.

TypeId TypeId TypeId TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(Inherited from Attribute)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

(Inherited from Attribute)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object) Match(Object) Match(Object) Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

显式界面实现

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

适用于