0

I want to list all methods from a WCF service that has the attribute "OperationContractAttribute"

For that, I use the following code:

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();

So, the OperationContractAttribute is specified in the interface (IClassA) and when I try to search this method attribute in the ClassA class, it can't find it however I specified the flag true for method GetCustomAttributes for searching to ancestors

Sergiu
  • 297
  • 3
  • 16
  • May this help you LINK to similar Q/A http://stackoverflow.com/questions/2720617/how-to-get-all-methods-from-wcf-service – Milan Raval Jan 30 '13 at 10:11
  • 1
    No, because I need ONLY OperationContract methods and not all methods (the solution from your link list all methods (including ToString, GetHashCode and so on...)) – Sergiu Jan 30 '13 at 10:19

2 Answers2

1

This will do

 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;
                }
            }
Milan Raval
  • 1,880
  • 1
  • 16
  • 33
  • What if a method has more than one attribute or does not have any at all? – DarkWanderer Jan 30 '13 at 11:37
  • it should be handled in the if : if(method.GetCustomAttributes(true).Any(attrib => ((System.Attribute)attrib).TypeId.ToString() == "System.ServiceModel.OperationContractAttribute")) – Milan Raval Jan 30 '13 at 11:58
0
webMethods = service.GetInterface(serviceContract).GetMethods().Where(
    method => method.GetCustomAttributes
      (typeof(OperationContractAttribute)).Any())
      .ToList();
fthiella
  • 48,073
  • 15
  • 90
  • 106
Sergiu
  • 297
  • 3
  • 16