Getting webservice methods from Soap Client using reflection
I'm trying to get a MethodInfo colle开发者_StackOverflow中文版ction from a SOAPClient object that contains only the methods of the webservice itself. Here is what I'm currently doing. At the moment it returns all the methods of the MyServiceSoapClient.
MyServiceSoapClient myService = new MyServiceSoapClient();
MethodInfo[] methods = myService.GetType().GetMethods();
The GetMethods() method supports bindingflags that you can use to more specifically select the methods you want it to return. Have a look here:
http://msdn.microsoft.com/en-us/library/4d848zkb.aspx
Also, you could use some linq to further specify what you are after:
MethodInfo[] methods = myService.GetType().GetMethods();
MethodInfo[] methodsOfWebservice = methods.Where(m => m.whatever == whatever && m.anothercondition == true); // etc.
The last option you have is to add an Attribute to every method you want it to return and then test for the presence of the Attribute. Have a look here:
http://www.codeproject.com/KB/cs/attributes.aspx
Update 2011-01-18
I've looked at Microsoft KnowledgeBase and found that the [WebMethod] is an attribute. http://support.microsoft.com/kb/308359 and http://msdn.microsoft.com/en-us/library/28a537td.aspx. When getting all methods you could test for the presence of this attribute to decide whether the method is a WebMethod or not.
List<MethodInfo> methodsOfWebservice = new List<MethodInfo>();
MethodInfo[] methods = myService.GetType().GetMethods();
foreach(MethodInfo method in methods)
{
foreach (Attribute attribute in method.GetCustomAttributes(true))
{
if (attribute is WebMethodAttribute)
methodsOfWebservice.Add(method);
}
}
Update 2011-01-20
I just tested the following code and it does in fact give me the the WebMethodAttribute
in the attribute
variable:
Type type = obj.GetType();
var method = type.GetMethod("methodname");
var attribute = method.GetCustomAttributes(typeof(WebMethodAttribute), true);
I'm sure you should be able to do the same with your code and test for the presence of the WebMethodAttribute
Here is my method with XML parsing:
First I download the XML from the WSDL:
private string GetPageSource(string url)
{
string htmlSource = string.Empty;
try
{
WebProxy myProxy = new WebProxy("ProxyAdress", 8080);
using (WebClient client = new WebClient())
{
client.Proxy = myProxy;
client.Proxy.Credentials = new NetworkCredential("Username", "Password");
htmlSource = client.DownloadString(url);
}
}
catch (WebException ex)
{
// log any exceptions
}
return htmlSource;
}
Then I am parsing the XML to the node where the methods for the SOAP-Client are and adding them to a generic list of strings:
_strXmlFromUrl = GetPageSource(_strWebserviceUrl); // My TestUrl: http://www.webservicex.net/globalweather.asmx?WSDL
XmlDocument xmlDoc = new XmlDocument();
XmlNamespaceManager nmspManager = new XmlNamespaceManager(xmlDoc.NameTable);
nmspManager.AddNamespace("wsdl", "http://schemas.xmlsoap.org/wsdl/");
xmlDoc.LoadXml(_strXmlFromUrl);
XmlNodeList methodNodes = xmlDoc.SelectNodes("//wsdl:portType/wsdl:operation[@name]", nmspManager);
List<string> lstMehtodNames = new List<string>();
for (int i = 0; i < methodNodes.Count; i++)
{
lstMehtodNames.Add(String.Concat(methodNodes[i].ParentNode.Attributes["name"].Value,": " ,methodNodes[i].Attributes[0].Value));
}
Have fun with it.
Taragneti
精彩评论