Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a Web Service Method?

Tags:

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

like image 673
Amc_rtty Avatar asked Aug 03 '09 21:08

Amc_rtty


People also ask

How do I call a webservice method in C#?

In the Project Type pane, select a Visual C# application. In the Templates pane, select a Windows Forms Application. Enter a project name such as CallingServiceExample, and browse to a storage location. In the Solution Explorer, right-click the project name and click Add Service Reference.

How do you query a web service?

Query Methods. The Query Web service includes two methods for retrieving search results: Query and QueryEx. Both methods accept the same input, a string that contains XML that specifies the search query parameters. However, the format of the returned results differs.

How do I run a web service?

After entering the WSDL URL or file, clicking Go to query available WSDL methods. The web service class which contains the available methods. After entering a WSDL location in the WSDL URI parameter, clicking Go will attempt to query available Web Service Types specified in the WSDL URL or file.

What are web service methods?

A Web service is a method of communication between two electronic devices over a network. It is a software function provided at a network address over the Web with the service always-on as in the concept of utility computing. Many organizations use multiple software systems for management.


1 Answers

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.

like image 177
James Conigliaro Avatar answered Sep 18 '22 15:09

James Conigliaro