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.
Download the code: DotnetFriendsClient.zip