Export (0) Print
Expand All

OperationContractAttribute Class

Indicates that a method defines an operation that is part of a service contract in a Windows Communication Foundation (WCF) application.

System.Object
  System.Attribute
    System.ServiceModel.OperationContractAttribute

Namespace:  System.ServiceModel
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

[AttributeUsageAttribute(AttributeTargets.Method)]
public sealed class OperationContractAttribute : Attribute

The OperationContractAttribute type exposes the following members.

  NameDescription
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsOperationContractAttributeInitializes a new instance of the OperationContractAttribute class.
Top

  NameDescription
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsActionGets or sets the WS-Addressing action of the request message.
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsAsyncPatternIndicates that an operation is implemented asynchronously using a Begin<methodName> and End<methodName> method pair in a service contract.
Public propertyHasProtectionLevelGets a value that indicates whether the messages for this operation must be encrypted, signed, or both.
Public propertyIsInitiatingGets 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).
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsIsOneWayGets or sets a value that indicates whether an operation returns a reply message.
Public propertyIsTerminatingGets 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.
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsNameGets or sets the name of the operation.
Public propertyProtectionLevelGets or sets a value that specifies whether the messages of an operation must be encrypted, signed, or both.
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsReplyActionGets or sets the value of the SOAP action for the reply message of the operation.
Public propertyTypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)
Top

  NameDescription
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public methodMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Explicit interface implemetationPrivate method_Attribute.GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoRetrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.InvokeProvides access to properties and methods exposed by an object. (Inherited from Attribute.)
Top

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).

Use the OperationContractAttribute properties to control the structure of the operation and the values expressed in metadata:

  • The Action property specifies the action that uniquely identifies this operation. WCF dispatches request messages to methods based on their action.

  • The AsyncPattern property indicates that the operation is implemented or can be called asynchronously using a Begin/End method pair.

  • The HasProtectionLevel property indicates whether the ProtectionLevel property has been explicitly set.

  • The IsOneWay property indicates that the operation only consists of a single input message. The operation has no associated output message.

  • The IsInitiating property specifies whether this operation can be the initial operation in a session.

  • The IsTerminating property specifies whether WCF attempts to terminate the current session after the operation completes.

  • The ProtectionLevel property specifies the message-level security that an operation requires at run time.

  • The ReplyAction property specifies the action of the reply message for the operation.

The OperationContractAttribute attribute declares that a method is an operation in a service contract. Only methods attributed with the OperationContractAttribute are exposed as service operations. A service contract without any methods marked with the OperationContractAttribute exposes no operations.

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). 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. For details, see AsyncPattern.

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. Methods of this kind cannot return a reply message so the method's declaration must return void.

Important noteImportant

When programmatically retrieving the information store in this attribute, use the ContractDescription class instead of reflection.

NoteNote

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. Using this approach enables the application and the infrastructure to send error information (such as a SOAP fault) back to the client. 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.

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. Use the IsInitiating property to prevent clients from calling a particular service operation prior to other operations. Use the IsTerminating property to have WCF close the channel after clients call a particular service operation. For more information, see Using Sessions.

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. For more information, see ProtectionLevel and Understanding Protection Level.

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
  }
}

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;
  }
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft