How to Consume a Web Service

Category: XML and Web Services

How to Consume a Web Service in C# [Part 1 of 2]

Introduction
In this article we will explain how you can consume a web service utilizing “.Net Web Service Studio” version 2.0. This is a free download and was written by a person or people at Microsoft… at least to the best of my knowledge and based on the “About” screen provided. It is a great little utility to help expedite the consumption of a web service.

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

Consuming a web service can be done in many ways and with various efforts. .Net Web Service Studio takes the lengthy proxy creation process and automates a lot of the steps. It does most of the work for you and can create the web service proxy as well as the client code that calls the web service proxy. You can even use this application to debug your web service along with testing that web service before you start to write any code against that web service to insure that the web service works correctly and that all of the methods you are looking for exist within that web service.

What we will learn in these articles:

  • The .Net Web Service Studio Application
  • Create a Web Service
  • Consuming a Web Service
  • Creating the Web Service Proxy
  • Creating the client code to call the proxy
  • Debugging the Web Service

Implementation
We will take you through the process of creating your first Web Service using Microsoft Visual Studio 2005 and C#. This is the first step in our process so we will have a Web Service that we can consume and show how we can consume that Web Service utilizing .Net Web Service Studio and Microsoft Visual Studio.

Requirements:
The .Net Web Service Studio 2.0 used to be on GotDotNet.com, however, since it has been shut down this application is becoming harder and harder to find. In a search done in July of 2008 I found that this application is readily available and I was able to locate it with no problem, the following URL is one of the locations where this application can be found:
http://mattharrah.com/blog/tag/web-services/
If you are not familiar with this application here is a small description taken from the readme file:
“.NET Webservice Studio is a tool to invoke webmethods interactively. The user can provide a WSDL endpoint. On clicking button Get the tool fetches the WSDL, generates .NET proxy from the WSDL and displays the list of methods available. The user can choose any method and provide the required input parameters. On clicking Invoke the SOAP request is sent to the server and the response is parsed to display the return value.
This tool is meant for webservice implementers to test their webservices without having to write the client code. This could also be used to access other webservices whose WSDL endpoint is known.”
Once you have downloaded the .Net Web Service Studio application, install the application onto your system so it is available for you to continue with this example. Here is what the application will look like when it is opened:

We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!

 

 

Fig. 1: .Net Studio Web Service Application


[Click to see full-size]


Now that you have installed the .Net Web Service Studio application we are ready to proceed with the example of how to consume a web service via this application. The first step is to create the web service that you want to consume; the following example shows you how to create a very simple web service.

 

Please Note:
Using this process to consume a web service is not specific to WinForms or WebForms development and can be utilized in both types of development. You are simply creating a proxy via the .Net Web Service Studio application and outputting it to a format that can be implemented as needed in any Visual Studio 2005 project.

Source Code for the Web Service
Start Visual Studio 2005 and create new Web Service project as illustrated in Fig. 2:
Fig. 2:


[Click to see full-size]

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.


When you select the “OK” button the project will be created and the following code will be inserted into the file called “Service1.asmx.cs”. This is the main entry point for the web service.

 


using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;

namespace WebService1
{
///


/// Summary description for Service1
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}

This simple example code contains a “Public” method called “HelloWorld()” that returns the string “Hello World”. For simplicity purposes we will use this web service as is to show that you can create a web service and access it with ease. In future articles we can delve into the realm of modifying the web service and the nuances of doing so.
We have now created the Web Service that we will consume via the .Net Web Service Studio application.
Before we proceed to the next step you will need to run the web service so it is resident in memory and accessible by our next step. You will need to note the location of the web service as you run the web service from within Visual Studio. You will be able to get the location via the URL in the browser, from which you could cut and paste that into the .Net Web Service Studio application in our next step.

Fig. 3:


 

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

The URL in Fig. 3 does not represent the same port as you will have on your system, and your URL will be different. We added this screen shot strictly for illustration purposes only, and to give you a visual idea of the format you will be seeing.
In part two of this article we will learn how to consume the web service we created within the .Net Web Service Studio Application.

 

What we have learned:
We have learned of a new way to consume a web service, we have introduced you to the .Net Web Service Studio application and we have explained what this application does in an effort to help you easily consume and debug web service issues.

Continue reading Part Two of this Article Series here

Acknowledgements:
I would like to thank Matt Harrah for seeing a great need and allowing free downloads of the .Net Web Service Studio application from his Rocket Surgery blog. Please visit this site; in my search for this application I was very impressed with what I found. I would also like to thank the creator or creators of .Net Web Service Studio 2.0 for creating a great little utility that has saved me hours of work over the years.

Attachments



Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!