WCF OperationContract - What's the point of Action and ReplyAction?

21,307

Solution 1

You only need the Action / ReplyAction properties if you want to customize those values in the messages (and they're reflected in the WSDL). If you don't have them, the default is <serviceContractNamespace> + <serviceContractName> + <operationName> for the Action, and <serviceContractNamespace> + <serviceContractName> + <operationName> + "Response" for ReplyAction.

The code below prints out the Action/ReplyAction properties of all operations in the service.

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();
    }
}

Solution 2

Sometimes the generated WSDL is just not suitable for you. One interesting thing you can also do is set Action = "*" to create a unrecognized message handler.

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

Share:
21,307
michael
Author by

michael

Updated on June 03, 2020

Comments

  • michael
    michael about 4 years
    [ServiceContract(Namespace = "http://schemas.mycompany.com/", Name = "MyService")]
    public interface IMyService
    {
        [OperationContract(Name = "MyOperation")
        OperationResponse MyOperation(OperationRequest request);
    }
    

    In this scenario, what is the point of the Action and ReplyAction ?


    Edit: I should clarify my question...

    How would my wsdl differ if I don't specify these parts? Won't it just use some combination of the namespace, service name and opeartion name anyways?

  • michael
    michael about 13 years
    Why would anyone want to create an "unrecognized message handler"?
  • michael
    michael about 13 years
    So pretty much if I did ReplyAction = "http://schemas.mycompany.com/MyService/MyOperation" that would be identical to it building it on it's own?
  • carlosfigueira
    carlosfigueira about 13 years
    No, the reply action would be "http://schemas.mycompany.com/MyService/MyOperationResponse"‌​. I'll update the answer with how to print out the action / reply action properties of the operation.
  • michael
    michael about 13 years
    Sorry, I meant Action and ReplyAction would be the same but with "Response" appended to it.
  • seekerOfKnowledge
    seekerOfKnowledge about 11 years
    You sure stumped him, michael! Nearly 2 years and he's still thinking about it! :P
  • Andrey Shchekin
    Andrey Shchekin about 11 years
    @michael I believe it is not an "unrecognized message handler", but "unrecognized message handler".