Share via


OperationContractAttribute.Action Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets or sets the WS-Addressing action of the request message.

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

Syntax

'Declaration
Public Property Action As String
public string Action { get; set; }

Property Value

Type: System.String
The action to use in generating the WS-Addressing Action header.

Exceptions

Exception Condition
ArgumentNullException

The value is nulla null reference (Nothing in Visual Basic).

Remarks

Use the Action property to control the action of the method's input message. Because Silverlight 5 uses this action to dispatch an incoming message to the appropriate method, messages used within a contract operation must have unique actions. The default action value is a combination of the contract namespace (the default value is "http://tempuri.org/"), the contract name (interface name or the class name, if no explicit service interface is used), the operation name, and an additional string ("Response") if the message is a correlated response. You can override this default with the Action property.

To indicate that a service operation handles all messages that the service receives but cannot be directed to a service operation, specify the value "*" (an asterisk). This type of operation, called an unmatched message handler, must have one of following method signatures, or a InvalidOperationException is thrown:

  • The service operation can take only a Message object and return a Message object.

  • The service operation can take only a Message object and return nothing (that is, return void).

NoteNote:

A service contract can have only one service operation with the Action property set to "*". Any group of service contracts hosted at the same listenUri that a service class implements can have many service operations with the Action property set to "*" when the IsInitiating property is set to false. However, only one of those service operations can have the Action property set to "*" and the IsInitiating property set to true.

For more information, see

IsInitiating.

Examples

    ' 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 an operation that handles all messages the service receives.
    [ServiceContract(
        Name = "SampleServiceContract2",
        Namespace = "http://microsoft.SL3.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; }
    }

Version Information

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

Platforms

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