Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

OperationContractAttribute Class

Indicates that a method defines an operation that is part of a service contract in a Silverlight application.

System.Object
  System.Attribute
    System.ServiceModel.OperationContractAttribute

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

'Declaration
<AttributeUsageAttribute(AttributeTargets.Method)> _
Public NotInheritable Class OperationContractAttribute _
	Inherits Attribute

The OperationContractAttribute type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneOperationContractAttributeInitializes a new instance of the OperationContractAttribute class.
Top

  NameDescription
Public propertySupported by Silverlight for Windows PhoneActionGets or sets the WS-Addressing action of the request message.
Public propertySupported by Silverlight for Windows PhoneAsyncPatternIndicates that an operation is implemented asynchronously using a Begin<methodName> and End<methodName> method pair in a service contract.
Public propertySupported by Silverlight for Windows PhoneIsOneWayGets or sets a value that indicates whether an operation returns a reply message.
Public propertySupported by Silverlight for Windows PhoneNameGets or sets the name of the operation.
Public propertySupported by Silverlight for Windows PhoneReplyActionGets or sets the value of the SOAP action for the reply message of the operation.
Top

  NameDescription
Public methodSupported by Silverlight for Windows PhoneEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodSupported by Silverlight for Windows PhoneFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodSupported by Silverlight for Windows PhoneGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodSupported by Silverlight for Windows PhoneMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneToStringReturns 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.


	' A service contract that is defined with an interface
	' using an aysnchronous pattern for Silverlight applications
	' and a synchronous pattern otherwise.
	<ServiceContract(Name := "SampleServiceContract1", Namespace := "http://microsoft.SL3.documentation")> _
	Public Interface IService1
#If SILVERLIGHT Then
		<OperationContract(AsyncPattern := True)> _
		Function BeginGetPerson(ByVal personId As Integer, ByVal callback As AsyncCallback, ByVal state As Object) As IAsyncResult

		Function EndGetPerson(ByVal result As IAsyncResult) As Person
#Else
		<OperationContract> _
		Function GetPerson(ByVal personId As Integer) As Person
#End If
	End Interface

	<DataContract> _
	Public Class Person
		<DataMember> _
		Public Name As String
		<DataMember> _
		Public Age As Integer

	End Class


	' A service contract defined with a class
	' for an operation that handles all messages the service receives.
	<ServiceContract(Name := "SampleServiceContract2", Namespace := "http://microsoft.SL3.documentation")> _
	Public Class CustomerService
		<OperationContract(Name := "SampleOperationContract1")> _
		Public Function CountUsers() As Integer
			Return 2
		End Function

		<OperationContract(Action := "*")> _
		Public Function GetUser(ByVal id As Integer) As User
			If id = 1 Then
				Return New User() With {.IsMember = True, .Name = "Paul", .Age = 24}
			Else
				Return New User() With {.IsMember = False, .Name = "John", .Age = 64}
			End If
		End Function
	End Class

	<DataContract> _
	Public Class User
		Private privateIsMember As Boolean
		<DataMember> _
		Public Property IsMember() As Boolean
			Get
				Return privateIsMember
			End Get
			Set(ByVal value As Boolean)
				privateIsMember = value
			End Set
		End Property

		Private privateName As String
		<DataMember> _
		Public Property Name() As String
			Get
				Return privateName
			End Get
			Set(ByVal value As String)
				privateName = value
			End Set
		End Property

		Private privateAge As Integer
		<DataMember> _
		Public Property Age() As Integer
			Get
				Return privateAge
			End Get
			Set(ByVal value As Integer)
				privateAge = value
			End Set
		End Property
	End Class


	' A service contract defined with a class
	' for different response patterns.
	<ServiceContract(Name := "SampleServiceContract3", Namespace := "http://microsoft.SL3.documentation")> _
	Public Class OneAndTwoWay
		' The client waits until a response message appears.
		<OperationContract> _
		Public Function MethodOne(ByVal x As Integer, <System.Runtime.InteropServices.Out()> ByRef y As Integer) As Integer
			y = 34
			Return 0
		End Function

		' The client waits until an empty response message appears.
		<OperationContract> _
		Public Sub MethodTwo(ByVal x As Integer)
			Return
		End Sub

		' 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 Sub MethodThree(ByVal x As Integer)
			Return
		End Sub
	End Class


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

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

Community Additions

Show:
© 2017 Microsoft