OperationContractAttribute.Action 属性   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置请求消息的 WS-Addressing 操作。
public:
 property System::String ^ Action { System::String ^ get(); void set(System::String ^ value); };public string Action { get; set; }member this.Action : string with get, setPublic Property Action As String属性值
生成 WS-Addressing Action 标头时要使用的操作。
例外
该值为 null。
示例
下面的示例演示了一个服务,该服务使用 Action 和 ReplyAction 属性显式控制输入和输出(或答复)消息的 SOAP 操作,并使用 Name 属性控制元数据中操作的名称。 最后,应用程序还会使用值为“*”的 Action 指示处理无法识别的消息的方法。
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
实现该协定的服务发送类似以下示例的消息:
<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 属性控制方法输入消息的操作。 由于 WCF 使用此操作将传入消息调度到适当的方法,因此协定操作中使用的消息必须具有唯一的操作。 默认操作值是协定命名空间的组合, (默认值为 "http://tempuri.org/") 、协定名称 (接口名称或类名,如果未) 使用显式服务接口,则操作名称和附加字符串 (“响应”) (如果消息是相关响应)。 您可以使用 Action 属性重写该默认值。
若要指示服务操作可处理该服务接收的所有消息,但又不能定向到服务操作,请指定值“*”(星号)。 这种类型的操作(称为不匹配的消息处理程序)必须具有下列方法签名之一,否则会引发 InvalidOperationException:
备注
服务协定只能有一个 Action 属性设置为“*”的服务操作。 当属性设置为“”时falseIsInitiating,服务类实现的同一 listenUri 托管的任何服务协定组都可以具有许多服务操作,Action该属性设置为“*”。 但是,只有其中一个服务操作可以将 Action 属性设置为“*”“,属性 IsInitiating 设置为 true。 如需了解详情,请访问 IsInitiating。