有谁知道在 WCF 中列出各种操作契约(Contract)的 UriTemplate 的方法?我想做的是以某种方式在 IntegrationTesting 启动自托管服务并循环遍历所有操作契约(Contract)并尽可能打印 UriTemplates。
请您参考如下方法:
你是说 Action 吗? OperationContract 上没有 UriTemplate 属性。
如果是,您可以使用反射获取类型的方法,并从每个方法获取 OperationContractAttribute 以获取其 Action 属性。
var methods = typeof (IService1).GetMethods();
IEnumerable<string> actions = methods.Where(
m => m.GetCustomAttributes(typeof (OperationContractAttribute), true).Count() > 0)
.Select(m =>
((OperationContractAttribute)m.GetCustomAttributes(typeof (OperationContractAttribute), true).First()).Action);
Console.WriteLine(string.Join("\r\n",actions.ToArray()));
编辑:正如 marc 提到的,您可能在 WebGet 之后,所以将 OperationContractAttribute 替换为 WebGetAttribute 并将 Action 替换为 UriTemplate 或您希望看到的任何属性。
