German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


Create a Web Service Client in .NET 3.0 using C#

Written by omerkamal on Mar 21, 2007
How to access a Web Service with ASP .NET 3.0 by using C-Sharp

What is a Web Service Client?

Download the code: DotnetFriendsClient.zip

A Web service client is any component or application that communicates with a Web service using SOAP messages, or a comparable messaging protocol. A Web service client can be a traditional client application. A client can also be another Web application. (In this situation, the Web application would consume the XML within the SOAP message, format it, and send the result back to an ultimate client — perhaps a Web browser.)

We need these following five basic steps to cerate a Web service.

1.  Create a proxy class for the Web service.

2.  Reference the proxy class in the client code.

3.  Create an instance of the proxy class in the client code.

4.  If anonymous access has been disabled for the Web application hosting the Web service, set the Credentials property of the proxy class.

5.  Call the method on the proxy class that corresponds to the Web service method with which you want to communicate.

For most clients, these steps differ only in how the proxy class is referenced and how the Web service client is deployed.

The following Model describes how a web service client talks with the Web service server.

Now, we will go creating a web service client. To study about creating a Web Service please read following Articles:

1. Database Access with Web Service using Visual Studio
2. Walkthrough: Building a Basic XML Web Service Using ASP.NET

Add the Web service reference to the Windows-based application

You can add a reference to a Web service by using Visual Studio, or you can generate the proxy for the Web service by using the Wsdl.exe tool (WSDL), and then by using that proxy to access the Web service in your Windows-based application.

There are Two ways through which you can add the Reference of the web service to your Application:

1. Using Visual Studio

2. Using WSDL Tool

1. By using Visual Studio

Create a Windows-based application

1.

Start Visual Studio 2005 or Visual Studio .NET.

2.

On the File menu, point to New, and then click Project.

3.

Under Project Type, click to select Visual C# Project.

4.

Under Template, click to select Windows Application.

5.

In the Name box, type DotnetFriendsClient, and then click OK.

By default, a form that is named Form1 is created.

6.

Add a Button control to Form1.

Add a Reference to the Project

1.

In Solution Explorer, right-click DotnetFriendsClient, and then click Add Web Reference.

The Add Web Reference dialog box appears.

2.

In the Address bar, type the URL of the Webservice https://dotnet-friends.com/LatestService.asmx.

Now, Click the Go Button to access the Web Service. In the Web reference name change the default name to TestWebService.

 

3.

Press ENTER or click Add Reference.

After the reference is added you will notice some new refrences in your Project.

 

4.

In Solution Explorer, right-click Form1, and then click View Code.

5.

Add the following namespace at the top of the Form1.cs file.

Using DotnetFriendsClient.TestWebService;

Note: TestWebService is the name of the Web reference as it appears under Web References in Solution Explorer.

 

2. By using the WSDL Tool

As described earlier, to consume a web service, you need to cerate a proxy class. Use the following command line (in your command prompt) to cerate the Proxey:

wsdl "https://dotnet-friends.com/websevices.asmx" /out:myProxey.cs

After that, you will find a file hello.cs in the current directory created by the wsdl utility, which come with the .NET Framework. To consume it, you must compile it as a DLL:

csc myProxey.cs /t:library /out: myProxey.dll /r:System.Web.Service.dll /r:System.Xml.dll

Remember to reference the System.Web.Service.dll and System.Xml.dll libraries.


Accessing the Web Services in an Application
Put it into the /bin directory in your web server. Finally you can consume the web service through the following simple ASP.NET code.


Consume the Web service in the Windows-based application

1.

In Solution Explorer, right-click Form1, and then click View Code.

2.

Add Form1_Load to the Form1 and add a variable lstservice. your Form1 code will look like this:

      private LatestService lstservice;

       public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            lstservice = new LatestService();

        }      

Now add a new Label (labelWelcomeMsg) and a Button (Name: BtnLoad , Text: Load Service ). Also add the following line to the Button click event:

private void btnLoad_Click(object sender, EventArgs e)

 {

    labelWelcomeMsg.Text=lstservice.Welcome();

 }

    

3.

On the Debug menu, click Start to run the application.

4.

Click Load Service. Now you will see the welcome message.

Now, add a DataGridView to the Form. Uncheck adding, Editing and Deleting from the Gridview Tasks (Web Services are not meant to edit and Delete).

Add the following line of code to the Button Load event.

dataGridView1.DataSource = lstservice.GetLatestASPDOTNET().Tables[0];

dataGridView1.Update();

Thats all. Now you can Run the program aagin and after clicking Load service button you will be able to view the Data (you can change the style of the gridview according to your tast). This how our Form look like.



Services

 

Download the code: DotnetFriendsClient.zip

Visitors/Readers Comments
(for questions please use The Forum)



saikat

i have tried many times to add the web reference using the steps above but it fails.

 was trying to use a third party web service in my project and I have run into an issue. While adding the URL as a web reference I get the message

 

There was an error downloading 'http://www.webservicex.net/CreditCard.asmx?WSDL'.

 

The underlying connection was closed: The remote name could not be resolved.

 

 

I have disabled automatic detection of proxy setting in my browser and used explicit proxy address and port.

I have already added the requisite code for proxy server default but even this doesn’t resolve this.

 

 

<system.net>

            <defaultProxy>

                  <proxy usesystemdefaults="false" proxyadddress="http://10.122.3.98:80" bypassonlocal="true">proxy>

            defaultProxy>

      system.net>

 

Please help with some pointers

 

07/09/2007 05:42:31 UTC

D0n1_86_@hotm....
It is a userful software....

29/12/2007 19:46:58 UTC

Robert
i have worked on http://www.beijingticketing.com its a secure and reliable site for olympic tickets.

09/01/2008 04:30:09 UTC

Deepak
Very nice code.  Works great and fast way to learn Web Services, and how to create and consume.

11/01/2008 20:20:35 UTC

Harish
well I got it using web refernce method, but I am struck up with the other method i.e generating dll using wsdl. I have created the dll and added it to client aplliation bin folder. How do I create an object and call method.

23/01/2008 06:08:26 UTC

sanni

hello Saikat,

                       do one thing, use system name instead of IP address. try it that would be solved.

 

31/01/2008 13:08:34 UTC

cuongnx

I use webservice by java, why don't add web reference ... ?

10/04/2008 02:57:06 UTC

shaan
Very nice code and good examples to learn how to consume web service.

30/04/2008 19:20:53 UTC

LB
Hi!

Is there a way to consume the Web Service from another Web Service without having to do
WebService ws = new WebService();

but instead using a string with the service's name or url?
My problem is that I have a database with the Web Service's string and I need that so I can have
each method in the Web Service call different Web Services...

30/05/2008 11:19:48 UTC

sriram
http:// ur system ip address/your webservices foldername/xyz.asmx
like
htt:// 10.101.1.12/abcd/service.asmx

try plz

31/10/2008 02:01:16 UTC

kudus
give me a proxey please    

03/01/2009 11:55:10 UTC

Nareshkumar
Thank you for are given example above..
so many days i am trying to do using web services in Client application by using console application.. but am getting error. today i got the result..
Thanks  a lot ..

09/01/2009 06:18:38 UTC




Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)