How can I show all the methods that are available in my WCF in a dropdown

点点圈 提交于 2020-02-05 04:35:29

问题


How can I show all the methods that are available in my WCF in a dropdown. I need to show only those methods that are exposed to the client. I have the following code working, but it displays much more methods than expected. Seems it displays all.

MethodInfo[] methods = typeof(IntlService.ClientDataServiceClient).GetMethods();

//// sort methods by name
Array.Sort(methods,
        delegate(MethodInfo methods1, MethodInfo methods2)
        { return methods1.Name.CompareTo(methods2.Name); });

foreach (var method in methods)
{
    string methodName = method.Name;
    ddlMethods.Items.Add(methodName);
}

How can I restrict the display to show only the ones that I defined


回答1:


If you're looking to get only methods defined by your class, in this case, IntlService.ClientDataServiceClient, then alter your call to GetMethods() like this:

MethodInfo[] methods = typeof(IntlService.ClientDataServiceClient).GetMethods(BindingFlags.DeclaredOnly);

If you're looking to get only methods that are declared as service methods, then you'll need to examine the attributes on the methods:

MethodInfo[] methods = typeof(IntlService.ClientDataServiceClient).GetMethods(BindingFlags.DeclaredOnly);

// sort here...

foreach( var method in methods )
{
    if( method.GetCustomAttributes(typeof(System.ServiceModel.OperationContractAttribute), true).Length == 0 )
        continue;

    string methodName = method.Name;
    ddlMethods.Items.Add(methodName);
}



回答2:


foreach (var method in methods)
{
   // Add the line below
   if (method.GetCustomAttributes(typeof(OperationContractAttribute)).Length > 0)
   {
      string methodName = method.Name;
      ddlMethods.Items.Add(methodName);
   }
}


来源:https://stackoverflow.com/questions/4169096/how-can-i-show-all-the-methods-that-are-available-in-my-wcf-in-a-dropdown

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!