从WCF获取ServiceContract方法

时间:2013-01-30 09:34:30

标签: wcf operationcontract

我想列出具有属性“OperationContractAttribute”的WCF服务的所有方法

为此,我使用以下代码:

var service = assembly.GetType(typeName);
        if (service == null)
            return webMethodsInfo;
        var methodsInfo = service.GetMethods();
        webMethods = methodsInfo.Where(method => method.GetCustomAttributes
             (typeof(OperationContractAttribute), true).Any()).ToList();

因此,在接口(IClassA)中指定了OperationContractAttribute,当我尝试在ClassA类中搜索此方法属性时,它无法找到它,但我为方法GetCustomAttributes指定了标志为true以搜索祖先

2 个答案:

答案 0 :(得分:1)

这样做

 MethodInfo[] methods = typeof(ITimeService).GetMethods();

            foreach (var method in methods)
            {
                if (((System.Attribute)(method.GetCustomAttributes(true)[0])).TypeId.ToString() == "System.ServiceModel.OperationContractAttribute")
                {                 
                    string methodName = method.Name;
                }
            }

答案 1 :(得分:0)

webMethods = service.GetInterface(serviceContract).GetMethods().Where(
    method => method.GetCustomAttributes
      (typeof(OperationContractAttribute)).Any())
      .ToList();