Amc_rtty
Amc_rtty

Reputation: 3813

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 :)

Upvotes: 23

Views: 117647

Answers (4)

user5649168
user5649168

Reputation: 1

write return(secondmethod) inside of the first method

enter image description here

Upvotes: -1

John Saunders
John Saunders

Reputation: 161831

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.

Upvotes: 12

Steven Sudit
Steven Sudit

Reputation: 19630

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.

Upvotes: 1

James Conigliaro
James Conigliaro

Reputation: 3827

In visual studio, use the "Add Web Reference" feature and then enter in the URL of your web service.

By adding a reference to the DLL, you not referencing it as a web service, but simply as an assembly.

When you add a web reference it create a proxy class in your project that has the same or similar methods/arguments as your web service. That proxy class communicates with your web service via SOAP but hides all of the communications protocol stuff so you don't have to worry about it.

Upvotes: 20

Related Questions