wcfproxyoperationcontracts

List of operation contracts from wcf proxy


I probably need to search or investigate more. But thought of asking you guys first.. I have couple of WCF services hosted in Windows and in the client side I have the proxy with all these service contracts. My application is consuming them and which is just working fine. Now I wanted to know if there any way I can get the list of operations from each Contract if I give the service end point / something else that I have.

End pint

http://localhost:8080/myservice/Echo

Proxy

[ServiceContract]
public interface IEcho
{
    string Message { [OperationContract]get; [OperationContract]set; }
    [OperationContract]
    string SendEcho();
}

I need a method which would get me the list of operation inside the service contract... In this case List opeartions = SendEcho(); how do I get this point?


Solution

  • I wrote a sample code as follows, but you need to create the Echo service in the same assembly with your service which you want to get the list of methods:

    public System.Collections.Generic.List<string> SendEcho()
        {
            // Get the current executing assembly and return all exported types included in it:
            Type[] exportedTypes = System.Reflection.Assembly.GetExecutingAssembly().GetExportedTypes();
    
            // The list to store the method names
            System.Collections.Generic.List<string> methodsList = new System.Collections.Generic.List<string>();
    
            foreach (Type item in exportedTypes)
            {
                // Check the interfaces implemented in the current class:
                foreach (Type implementedInterfaces in item.GetInterfaces())
                {
                    object[] customInterfaceAttributes = implementedInterfaces.GetCustomAttributes(false);
                    if (customInterfaceAttributes.Length > 0)
                    {
                        foreach (object customAttribute in customInterfaceAttributes)
                        {
                            // Extract the method names only if the current implemented interface is marked with "ServiceContract"
                            if (customAttribute.GetType() == typeof(System.ServiceModel.ServiceContractAttribute))
                            {
                                System.Reflection.MemberInfo[] mi = implementedInterfaces.GetMembers();
    
                                foreach (System.Reflection.MemberInfo member in mi)
                                {
                                    if (System.Reflection.MemberTypes.Method == member.MemberType)
                                    {
                                        // If you want to have an idea about the method parameters you can get it from here: (count, type etc...)
                                        System.Reflection.ParameterInfo[] pi = ((System.Reflection.MethodInfo)member).GetParameters();
    
                                        // Check the method attributes and make sure that it is marked as "OperationContract":
                                        object[] methodAttributes = member.GetCustomAttributes(false);
                                        if (methodAttributes.Length > 0 && methodAttributes.Any(p => p.GetType() == typeof(System.ServiceModel.OperationContractAttribute)))
                                            methodsList.Add(member.Name);
                                    }
                                }
                            }
    
                        }
                    }
                }
            }
    
            return methodsList;
        }
    

    Hope this helps!