OperationContractAttribute.Action 属性

定义

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

public:
 property System::String ^ Action { System::String ^ get(); void set(System::String ^ value); };
public string Action { get; set; }
member this.Action : string with get, set
Public Property Action As String

属性值

String

生成 WS-Addressing Action 标头时要使用的操作。The action to use in generating the WS-Addressing Action header.

例外

该值为 nullThe value is null.

示例

下面的示例演示了一个服务,该服务使用 ActionReplyAction 属性显式控制输入和输出(或答复)消息的 SOAP 操作,并使用 Name 属性控制元数据中操作的名称。The following example is a service that uses the Action and ReplyAction properties to explicitly control the SOAP actions of both the input and output (or reply) messages, and the Name property to control the name of the operation in metadata. 最后,应用程序还会使用值为“*”的 Action 指示处理无法识别的消息的方法。Finally, the application also uses an Action value of "*" to indicate a method that handles unrecognized messages.

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;

namespace Microsoft.WCF.Documentation
{
  [ServiceContract(Namespace="http://Microsoft.WCF.Documentation")]
  public interface ISampleService{

    [OperationContract(
      Action="http://Microsoft.WCF.Documentation/OperationContractMethod",
      Name="OCAMethod",
      ReplyAction="http://Microsoft.WCF.Documentation/ResponseToOCAMethod"
    )]
    string SampleMethod(string msg);

    [OperationContractAttribute(Action = "*")]
    void UnrecognizedMessageHandler(Message msg);
  }

  class SampleService : ISampleService
  {
    public string  SampleMethod(string msg)
    {
      Console.WriteLine("Called with: {0}", msg);
        return "The service greets you: " + msg;
    }

    public void UnrecognizedMessageHandler(Message msg)
    {
      Console.WriteLine("Unrecognized message: " + msg.ToString());
    }
  }
}
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.Text

Namespace Microsoft.WCF.Documentation
  <ServiceContract(Namespace:="http://Microsoft.WCF.Documentation")> _
  Public Interface ISampleService

        <OperationContract(Action:="http://Microsoft.WCF.Documentation/OperationContractMethod", _
                           Name:="OCAMethod", ReplyAction:="http://Microsoft.WCF.Documentation/ResponseToOCAMethod")> _
        Function SampleMethod(ByVal msg As String) As String

    <OperationContractAttribute(Action := "*")> _
    Sub UnrecognizedMessageHandler(ByVal msg As Message)
  End Interface

  Friend Class SampleService
      Implements ISampleService
    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

    Public Sub UnrecognizedMessageHandler(ByVal msg As Message) Implements ISampleService.UnrecognizedMessageHandler
      Console.WriteLine("Unrecognized message: " & msg.ToString())
    End Sub
  End Class
End Namespace

实现该协定的服务发送类似以下示例的消息:A service that implements this contract sends messages that look like the following example:

<s:Envelope xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
  <s:Header>
    <a:Action s:mustUnderstand="1">http://Microsoft.WCF.Documentation/ResponseToOCAMethod</a:Action>
  </s:Header>
  <s:Body>
    <OCAMethodResponse xmlns="http://Microsoft.WCF.Documentation">
      <OCAMethodResult>The service greets you: Hello!</OCAMethodResult>
    </OCAMethodResponse>
  </s:Body>
</s:Envelope>

注解

使用 Action 属性控制方法输入消息的操作。Use the Action property to control the action of the method's input message. 由于 WCF 使用此操作将传入消息调度到相应的方法,因此协定操作中使用的消息必须具有唯一的操作。Because WCF uses this action to dispatch an incoming message to the appropriate method, messages used within a contract operation must have unique actions. 默认操作值是协定命名空间的组合 (默认值为 "http://tempuri.org/") ,协定名称 (接口名称或类名称,如果不使用显式服务接口) ,则操作名称和其他字符串 ( "Response" ) 如果消息是关联的响应。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. 您可以使用 Action 属性重写该默认值。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). 这种类型的操作(称为不匹配的消息处理程序)必须具有下列方法签名之一,否则会引发 InvalidOperationExceptionThis type of operation, called an unmatched message handler, must have one of following method signatures, or a InvalidOperationException is thrown:

  • 该服务操作只能接受一个 Message 对象,并且返回一个 Message 对象。The service operation can take only a Message object and return a Message object.

  • 该服务操作只能接受一个 Message 对象,并且不返回任何内容(即返回 void)。The service operation can take only a Message object and return nothing (that is, return void).

备注

服务协定只能有一个 Action 属性设置为“*”的服务操作。A service contract can have only one service operation with the Action property set to "*". Action * 当 IsInitiating 属性设置为时,服务类实现的相同 listenUri 中承载的任何服务协定组可具有多个属性设置为 "" 的服务操作 falseAny 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. 但是,只有其中一个服务操作可以将 Action 属性设置为 " * ",并将 IsInitiating 属性设置为 true。However, only one of those service operations can have the Action property set to "*" and the IsInitiating property set to true. 如需了解详情,请访问 IsInitiatingFor more details, see IsInitiating.

适用于