0

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);
NoBullMan
  • 2,032
  • 5
  • 40
  • 93
  • 2
    Does [this answer](https://stackoverflow.com/a/540075/3034273) your question? – Xerillio May 25 '21 at 18:51
  • Dumb question, just need to make sure: would 'this' in "Type thisType = this.GetType(); "in the link you provided would translate to 'ps.GetType()' in my post, when user clicks "Submit"? – NoBullMan May 26 '21 at 01:39
  • @Xerillio I updated my post with the solution based on your suggestion. Change your comment to an answer and I will mark it so. Thanks for the help. – NoBullMan May 26 '21 at 12:50
  • Good to hear you found a solution. I don't really think my comment qualifies as an answer - it's just a link to a duplicate question. – Xerillio May 26 '21 at 16:03

0 Answers0