2011-06-24 98 views
6
[ServiceContract(Namespace = "http://schemas.mycompany.com/", Name = "MyService")] 
public interface IMyService 
{ 
    [OperationContract(Name = "MyOperation") 
    OperationResponse MyOperation(OperationRequest request); 
} 

在這種情況下,ActionReplyAction有什麼意義?WCF OperationContract - Action和ReplyAction的作用是什麼?


編輯:我要澄清我的問題...

如何將我的WSDL不同,如果我不指定這些零件?無論如何,它不會僅僅使用名稱空間,服務名稱和操作名稱的組合嗎?

回答

7

如果您想要自定義消息中的這些值(並且它們反映在WSDL中),則只需要Action/ReplyAction屬性。如果您沒有它們,Action的默認值爲<serviceContractNamespace> + <serviceContractName> + <operationName>,ReplyAction的默認值爲<serviceContractNamespace> + <serviceContractName> + <operationName> + "Response"

下面的代碼打印出服務中所有操作的Action/ReplyAction屬性。

public class StackOverflow_6470463 
{ 
    [ServiceContract(Namespace = "http://schemas.mycompany.com/", Name = "MyService")] 
    public interface IMyService 
    { 
     [OperationContract(Name = "MyOperation")] 
     string MyOperation(string request); 
    } 
    public class Service : IMyService 
    { 
     public string MyOperation(string request) { return request; } 
    } 
    public static void Test() 
    { 
     string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; 
     ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); 
     host.AddServiceEndpoint(typeof(IMyService), new BasicHttpBinding(), ""); 
     host.Open(); 
     Console.WriteLine("Host opened"); 

     foreach (ServiceEndpoint endpoint in host.Description.Endpoints) 
     { 
      Console.WriteLine("Endpoint: {0}", endpoint.Name); 
      foreach (var operation in endpoint.Contract.Operations) 
      { 
       Console.WriteLine(" Operation: {0}", operation.Name); 
       Console.WriteLine(" Action: {0}", operation.Messages[0].Action); 
       if (operation.Messages.Count > 1) 
       { 
        Console.WriteLine(" ReplyAction: {0}", operation.Messages[1].Action); 
       } 
      } 
     } 

     Console.Write("Press ENTER to close the host"); 
     Console.ReadLine(); 
     host.Close(); 
    } 
} 
+0

所以相當多,如果我做了'ReplyAction = 「http://schemas.mycompany.com/MyService/MyOperation」'這將是相同的它建立在它自己的? – michael

+1

不,回覆操作應該是「http://schemas.mycompany.com/MyService/MyOperationResponse」。我將通過如何打印操作的操作/回覆操作屬性來更新答案。 – carlosfigueira

+0

對不起,我的意思是'Action'和'ReplyAction'是相同的,但附加了「Response」。 – michael

1

有時生成的WSDL不適合你。你也可以做一件有趣的事情是設置Action =「*」來創建一個無法識別的消息處理程序。

http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontractattribute.action.aspx

+1

爲什麼有人想創建一個「無法識別的消息處理程序」? – michael

+3

你確定難倒了他,邁克爾!將近2年,他仍在思考它! :P – seekerOfKnowledge

+6

@michael我相信它不是「無法識別的_message handler_」,而是「_unrecognized message_handler」。 –

0

行動定義您輸入URI爲您服務方法的SOAP操作。

ReplyAction定義了輸出uri爲您的服務方法。

它們基本上用於定製兩者的uri。

[ServiceContract] 
public partial interface IServiceContract 
{ 
    [OperationContract(
      Action = "http://mynamspace/v1/IServiceContract/Input/ServiceMethod", 
      ReplyAction = "http://mynamspace/v1/IServiceContract/Output/ServiceMethod")] 
    SomeResponseType ServiceMethod(SomeRequestType x); 

您的WSDL中,你會看到

<wsdl:portType name="IServiceContract"> 
    <wsdl:operation name="ServiceMethod"> 
    <wsdl:input wsaw:Action="http://mynamspace/v1/IServiceContract/Input/ServiceMethod" name="SomeRequestType" message="tns:SomeRequestType " /> 
    <wsdl:output wsaw:Action="http://mynamspace/v1/IServiceContract/Output/ServiceMethod" name="SomeResponseType" message="tns:SomeResponseType " /> 
相關問題