OperationContractAttribute Class
TOC
Collapse the table of content
Expand the table of content

OperationContractAttribute Class

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Indicates that a method defines an operation that is part of a service contract in a Windows Phone 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 methodOperationContractAttributeInitializes a new instance of the OperationContractAttribute class.
Top

  NameDescription
Public propertyActionGets or sets the WS-Addressing action of the request message.
Public propertyAsyncPatternIndicates that an operation is implemented asynchronously using a Begin<methodName> and End<methodName> method pair in a service contract.
Public propertyIsOneWayGets or sets a value that indicates whether an operation returns a reply message.
Public propertyNameGets or sets the name of the operation.
Public propertyReplyActionGets or sets the value of the SOAP action for the reply message of the operation.
Top

  NameDescription
Public methodEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
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 following 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.

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

  • 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 client to implement operations asynchronously is a client implementation detail and is not exposed in metadata (such as Web Services Description Language (WSDL)). 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 more information, see AsyncPattern.

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.

Capabilities

If you use this API in your app, you must specify the following capabilities in the app manifest. Otherwise, your app might not work correctly or it might exit unexpectedly.

ID_CAP_NETWORKING

Windows Phone 8, Windows Phone OS 7.1

For more info, see App capabilities and hardware requirements for Windows Phone 8.


    // A service contract that is defined with an interface
    // using an aysnchronous pattern for Windows Phone applications
    // and a synchronous pattern otherwise.
    [ServiceContract(
        Name = "SampleServiceContract1",
        Namespace = "http://microsoft.wp.documentation")]
    public interface IService1
    {
#if WINDOWS_PHONE
        [OperationContract(AsyncPattern = true)]
        IAsyncResult BeginGetPerson(int personId, AsyncCallback callback, object state);

        Person EndGetPerson(IAsyncResult result);
#else
        [OperationContract]
        Person GetPerson(int personId);
#endif
    }

    [DataContract]
    public class Person
    {
        [DataMember]
        public string Name;
        [DataMember]
        public int Age;

    }


    // A service contract defined with a class
    // for an operation that handles all messages the service receives.
    [ServiceContract(
        Name = "SampleServiceContract2",
        Namespace = "http://microsoft.wp.documentation")]
    public class CustomerService
    {
        [OperationContract(Name = "SampleOperationContract1")]
        public int CountUsers()
        {
            return 2;
        }

        [OperationContract(Action = "*")]
        public User GetUser(int id)
        {
            if (id == 1)
            {
                return new User() { IsMember = true, Name = "Paul", Age = 24 };
            }
            else
            {
                return new User() { IsMember = false, Name = "John", Age = 64 };
            }
        }
    }

    [DataContract]
    public class User
    {
        [DataMember]
        public bool IsMember { get; set; }

        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public int Age { get; set; }
    }


    // A service contract defined with a class
    // for different response patterns.
    [ServiceContract(
        Name = "SampleServiceContract3",
        Namespace = "http://microsoft.wp.documentation")]
    public class OneAndTwoWay
    {
        // The client waits until a response message appears.
        [OperationContract]
        public int MethodOne(int x, out int y)
        {
            y = 34;
            return 0;
        }

        // The client waits until an empty response message appears.
        [OperationContract]
        public void MethodTwo(int x)
        {
            return;
        }

        // The client returns as soon as an outbound message
        // is queued for dispatch to the service; no response
        // message is generated or sent.
        [OperationContract(IsOneWay = true)]
        public void MethodThree(int x)
        {
            return;
        }
    }


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

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:
© 2017 Microsoft