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