views:

430

answers:

3

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. Thank you in advance!

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

+4  A: 

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.

James Conigliaro
Superb - thank you so much! I have now removed the dll ref , and added a Service Reference to the URL. I have however noticed it does not give me direct access to my method, instead I get access to classes called: GetFileListOnWebServerRequest, GetFileListOnWebServerRequestBody, GetFileListOnWebServerResponse, GetFileListOnWebServerResponseBody. is there any web resource I can read regarding these autogenerated classes? I don't know how to use these.
Andrei
It generates lots of parts, but just look for the GetFileListOnWebServer method itself.
Steven Sudit
Actually, just look at John's example.
Steven Sudit
Oops sorry, I answered myself - just had to select webservice instead of service reference.I would rate your post as helpful but I can't yet.Peace!
Andrei
A: 

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.

Steven Sudit
Thank you for your answer, Steven! I will certainly look into WCF. However as for this task, its a "homework" asignment so I was told to use asmx :)Cheers!
Andrei
Ok, but if it's homework, please tag it as such. I've taken the liberty of doing this for you.
Steven Sudit
+2  A: 

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.

John Saunders
Thanks a lot John, I have managed to locate the methods based on your example.
Andrei