WCF的OperationContract - Action和ReplyAction有什么用处?

10
[ServiceContract(Namespace = "http://schemas.mycompany.com/", Name = "MyService")]
public interface IMyService
{
    [OperationContract(Name = "MyOperation")
    OperationResponse MyOperation(OperationRequest request);
}
在这种情况下,ActionReplyAction的作用是什么?

编辑:我应该澄清我的问题...

如果我不指定这些部分,我的wsdl会有什么不同?它不会只是在命名空间、服务名称和操作名称之间使用一些组合吗?

2个回答

9
你只需要使用Action/ReplyAction属性,如果你想要在消息中自定义这些值(并且它们反映在WSDL中)。如果你没有它们,那么默认值为<serviceContractNamespace> + <serviceContractName> + <operationName>用于Action,以及<serviceContractNamespace> + <serviceContractName> + <operationName> + "Response"用于ReplyAction。
下面的代码打印出服务中所有操作的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();
    }
}

如果我这样做 ReplyAction = "http://schemas.mycompany.com/MyService/MyOperation",那么它就和自己构建是一样的了吗? - michael
1
不,回复操作将是“http://schemas.mycompany.com/MyService/MyOperationResponse”。我会更新答案,告诉你如何打印操作的动作/回复动作属性。 - carlosfigueira
抱歉,我的意思是ActionReplyAction将是相同的,但会在其后添加“Response”。 - michael

1

1
为什么有人想要创建一个“未识别的消息处理程序”? - michael
4
你真让他难堪,迈克尔!快2年了,他还在思考呢!:P - seekerOfKnowledge
9
我认为这不是“未被识别的信息处理程序”,而是“未被识别的消息处理程序”。 - Andrey Shchekin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接