[c#] How to call a Web Service Method?

I have a web service that contains this method:

[WebMethod]
public static List<string> GetFileListOnWebServer()
{
   DirectoryInfo dInfo = new DirectoryInfo(HostingEnvironment.MapPath("~/UploadedFiles/"));
   FileInfo[] fInfo = dInfo.GetFiles("*.*", SearchOption.TopDirectoryOnly);

   List<string> listFilenames = new List<string>(fInfo.Length);

   for(int i = 0; i < fInfo.Length; i++)
   {
        listFilenames.Add(fInfo[i].Name);
   }

   return listFilenames;
}

This returns a list of filenames in a folder. When i debug the application, it works fine.

What I want to do, is to call this webservice method from a winform application. I added a reference to the .dll of the webservice, and this is how I call the above method:

private void Form1_Load(object sender, EventArgs e)
{
    List<string> files = TestUploaderWebService.Service1.GetFileListOnWebServer();
}

The above code does not work - when it enters the method, the path of the web app is null, and lots of properties from HostingEnvironment class are also null. Where is my mistake, in trying to call a web service method from another winform app?

Please note that the web service is made in Visual Web Developer Express, and the winform in Visual C# express; this is why I had to add the web service dll as a reference in the winform app. I do not have Visual Studio full, which would have allowed me a single solution with both projects.

I am new to web services.

PS - i love the formatting of text on-the-fly here :)

This question is related to c# winforms web-services

The answer is


The current way to do this is by using the "Add Service Reference" command. If you specify "TestUploaderWebService" as the service reference name, that will generate the type TestUploaderWebService.Service1. That class will have a method named GetFileListOnWebServer, which will return an array of strings (you can change that to be a list of strings if you like). You would use it like this:

string[] files = null;
TestUploaderWebService.Service1 proxy = null;
bool success = false;
try
{
    proxy = new TestUploaderWebService.Service1();
    files = proxy.GetFileListOnWebServer();
    proxy.Close();
    success = true;
}
finally
{
    if (!success)
    {
        proxy.Abort();
    }
}

P.S. Tell your instructor to look at "Microsoft: ASMX Web Services are a “Legacy Technology”", and ask why he's teaching out of date technology.


James' answer is correct, of course, but I should remind you that the whole ASMX thing is, if not obsolete, at least not the current method. I strongly suggest that you look into WCF, if only to avoid learning things you will need to forget.


write return(secondmethod) inside of the first method

enter image description here


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to winforms

How to set combobox default value? Get the cell value of a GridView row Getting the first and last day of a month, using a given DateTime object Check if a record exists in the database Delete a row in DataGridView Control in VB.NET How to make picturebox transparent? Set default format of datetimepicker as dd-MM-yyyy Changing datagridview cell color based on condition C# Inserting Data from a form into an access Database How to use ConfigurationManager

Examples related to web-services

How do I POST XML data to a webservice with Postman? How to send json data in POST request using C# org.springframework.web.client.HttpClientErrorException: 400 Bad Request How to call a REST web service API from JavaScript? The request was rejected because no multipart boundary was found in springboot Generating Request/Response XML from a WSDL How to send a POST request using volley with string body? How to send post request to the below post method using postman rest client How to pass a JSON array as a parameter in URL Postman Chrome: What is the difference between form-data, x-www-form-urlencoded and raw