代码之家  ›  专栏  ›  技术社区  ›  mhenrixon

列出wcf操作合同的uritemplate?

  •  1
  • mhenrixon  · 技术社区  · 15 年前

    有人知道在wcf中列出各种操作合同的uritemplate的方法吗?我想做的是在集成测试中,以某种方式启动一个自托管服务,并循环处理所有的操作契约,如果可能的话,打印uritemplates。

    1 回复  |  直到 15 年前
        1
  •  4
  •   Sky Sanders    15 年前

    你是说行动吗?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()));
    

    编辑:正如马克所提到的,你可能在webget之后,所以替换 OperationContractAttribute 具有 WebGetAttribute Action 具有 UriTemplate 或者任何你想看的财产。