Search code examples
wcfoperationcontract

Get ServiceContract Methods from WCF


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


Solution

  • 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;
                    }
                }