OperationContractAttribute.Action 屬性

定義

取得或設定要求訊息的 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, set
Public Property Action As String

屬性值

String

用來產生 WS-Addressing 動作標頭的動作。

例外狀況

值為 null

範例

下列範例是一個服務,它使用 ActionReplyAction 屬性來明確控制輸入與輸出 (或回覆) 訊息的 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

  • 服務作業只能讀取一個 Message 物件,並傳回一個 Message 物件。

  • 服務作業只能讀取一個 Message 物件,而不傳回任何東西 (也就是傳回 void)。

注意

服務合約中,只能有一個把 Action 屬性設為 "*" 的服務作業。 當 屬性設定為 時 false IsInitiating ,裝載于服務類別實作之相同 listenUri 的任何服務合約群組,都可以有許多服務作業,且 Action 屬性設定為 「 * 」。 不過,只有其中一個服務作業可以將 Action 屬性設定為 「 * 」,而 IsInitiating 屬性設定為 true。 如需詳細資訊,請參閱 IsInitiating

適用於