WCF OperationContract - Action和ReplyAction有什么意义?

时间:2011-06-24 15:57:17

标签: wcf soap action reply operationcontract

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

在这种情况下,ActionReplyAction是什么意思?


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

如果我没有指定这些部分,我的wsdl会有什么不同?它不会只是使用命名空间,服务名称和opeartion名称的某种组合吗?

2 个答案:

答案 0 :(得分:7)

如果要在消息中自定义这些值(并且它们反映在WSDL中),则只需要Action / ReplyAction属性。如果您没有它们,则操作的默认值为<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();
    }
}

答案 1 :(得分:1)

有时生成的WSDL不适合您。您还可以做的一件有趣的事情是设置Action =“*”来创建一个无法识别的消息处理程序。

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