I am testing a client application with a web reference to a web service; the web reference is called "PrinterStatus".
In this application I populate a dropdown list with list of available method names. I want to call the method user has selected and not sure how to set it up.
private void PopulateDropdown()
{
// web service URL
string url = "http://1.2.3.4/PS_WS/PrinterStatus.asmx?WSDL";
WebServiceInfo webServiceInfo = WebServiceInfo.OpenWsdl(new Uri(url));
foreach (WebMethodInfo method in webServiceInfo.WebMethods)
{
ListItem li = new ListItem(method.Name);
ddlMethods.Items.Add(li);
}
}
This populates the drop down list with list of available methods (the code for "WebServiceInfo" is at this link).
When user clicks "Submit", I want to be able to call:
PrinterStatus ps = new PrinterStatus();
string sResp = ps.<method_name_user_selected>();
Update
Following Xerillio's recommendation, I made the following changes to get this to work. In my test client app I have a dropdown populated with available method names (ddlMethods) and two textboxes for IP address and port number of printer whose status I am interested in (tbIP and tbPort) and a "submit" button. I have removed all validation stuff.
Also, the web reference created in client app is named "PrinterStatus". The methods in web service return a JSON object.
protected void btnSubmit_Click(object sender, EventArgs e)
{
string sMethodName = ddlMethods.SelectedValue;
string sIPAddress = tbIP.Text.Trim();
string sPort = tbPort.Text.Trim();
int iPort = int.Parse(sPort);
PrinterStatus ps = new PrinterStatus();
Type thisType = ps.GetType();
MethodInfo theMethod = thisType.GetMethod(sMethodName);
string sResp = theMethod.Invoke(ps, new object[] {sIPAddress, iPort}) as string;
Response resp = JsonConvert.DeserializeObject<Response>(sResp);