Skip navigation

Fill ’er Up with Web Services!

Service Station

Languages: VB | C#

Technologies: .NET | Web Services

 

Fill er Up with Web Services!

Getting Started with .NET Web Services

 

By Robert Lair

 

Web Services are a key component of Microsoft.NET. This is the first in an ongoing column that will cover a variety of topics concerning Web Services. In this first installment, we will discuss one of the many benefits of Web Services code reuse. You will learn how to build a Web Service as well as how to consume that Web Service from an application.

 

Reusing Code

Have you ever been responsible for implementing a cool new feature for an application that you may not have known exactly how to implement? Maybe you knew how but also knew that it would be very complex and time consuming. Did you wonder whether anyone else had solved this problem before? You may have searched the Internet or asked friends and coworkers. This is something developers do every day, and in fact, it is something they should do more often to avoid reinventing the wheel.

 

In the software development industry, more developers are starting to rely on code reuse in some capacity. Companies are building component libraries that contain common functionality that can be re-used across applications. Developers are posting code snippets online or to discussion groups so others can make use of their efforts. Companies too, are becoming more willing to share the code their employees have worked so hard to develop so others can use it in their own applications for a price. Developers are starting to see the purpose of code reuse and are beginning to realize that there is no point to reinventing code that has already been created and tested.

 

To utilize code that others had implemented, developers in the past had to incorporate it into their own application by copying the code snippet into their code and modifying it to fit their needs. Or perhaps they would be responsible for installing a component library on their server and have their applications access that library. This resulted in many versions of the code, possibly each slightly different, scattered all over the place. When the original developer decides to update or make a correction to the code, trickling down the changes to all the developers who have benefited from the original code can be a very difficult, if not impossible, task.

 

Wouldn t it be nice if we could keep the code in one central location and have everyone access it from there? Then the original creator could make enhancements or modifications and instantly provide updates to all the applications that utilize it. Great idea, but how can we do this? A Web Service is the answer. Let s look at a sample Web Service scenario so we can see how this is possible.

 

Using a Web Service

John Doe is responsible for creating an application that will need to collect user information. However, he needs a way to verify ZIP codes against cities and states the user has entered. John is not sure how he to go about this, so he decides to check if anyone else has already done it. John finds the ZipCodeResolver Web Service published by EraServer.NET at http://webservices.eraserver.net. The ZipCodeResolver service exposes a number of public Web methods that John can access from his application. When John accesses these methods, he is actually running the methods stored in the Web Service on EraServer.NET s server.

 

So how does John s server know to call a particular public method on a server thousands of miles away? A local proxy class is generated for the ZipCodeResolver service. The proxy class provides a wrapper that details the location and specifications of the public Web methods. John places the proxy class into the bin folder of an application, compiles it, and calls it as if it were a normal assembly. However, rather than containing the code itself, the compiled proxy class calls the Web Service to perform its work. John s application doesn t know the difference.

 

Creating a Web Service

As you ve seen, using a Web Service is pretty easy. Creating one isn t that much harder. Let s walk through creating a simple Web Service. The Web Service will contain two methods: Add and Subtract. This Web Service is not terribly useful; however, I wanted to focus on the steps used to create a Web Service, not on its implementation.

 

In creating a Web Service, you begin by creating a new file using a text editor. At the top of the new file, add the following WebService directive:

 

<%@ WebService Language="VB" Class="BasicMath" %>

 

Next we need to define our class. This is done in the same way as any other Microsoft .NET assembly with a few additional requirements. The class must be public, must import the System.Web.Services namespace, and should implement the WebService base class. In addition, the class should be given the attribute, which will define the namespace of the service. All Web Services must be given a namespace so there is some way to distinguish services from each other. If you leave out the WebService attribute, the temporary namespace http://tempuri.org will be assigned to your service. This is okay for testing purposes, but should be fixed before your service goes public. FIGURE 1 shows the starting point for our Web Service s class.

 

<%@ WebService Language="VB" Class="BasicMath" %>

 

Imports System

Imports System.Web.Services

 

 Namespace:="http://beta2.eraserver.net/servicestation/")> _

 Public Class BasicMath : Inherits WebService

 

   // Add Web Service methods here ...

 

End Class

FIGURE 1: WebService class definition.

 

Our Web Service is going to define the two methods, Add and Subtract. The implementation of these methods is trivial; however, there is one difference between methods exposed from a Web Service and methods that belong to a standard assembly. Methods that are exposed by a Web Service are known as WebMethods, and they must be defined as public methods that are marked with the attribute. FIGURE 2 shows our Web Service with the two WebMethods defined.

 

<%@ WebService Language="VB" Class="BasicMath" %>

 

Imports System

Imports System.Web.Services

 

 Namespace:="http://beta2.eraserver.net/servicestation/")> _

 Public Class BasicMath : Inherits WebService

 

    Public Function Add(a As Integer, _

    b As Integer) As Integer

       Return(a + b)

   End Function

 

    Public Function Subtract(a As Integer, _

    b As Integer) As Integer

       Return(a - b)

   End Function

 

End Class

FIGURE 2: BasicMath Web Service.

 

You must use the .asmx file extension for Web Services. Let s call ours BasicMath.asmx. The file should reside in an application root somewhere on your server. My service is stored on a server at EraServer.NET in the application root ServiceStation. You can access the Web Service simply by navigating to the Web Service s URL, http://beta2.eraserver.net/ServiceStation/BasicMath.asmx, with the results shown in FIGURE 3.

 


FIGURE 3: BasicMath.asmx Web Service.

 

When you click on one of the Web methods, you are taken to a test page, shown in FIGURE 4, which will allow you to execute the Web Service and check the result.

 


FIGURE 4: Test page for the Add Web method.

 

When you enter two numbers into the two input controls and press Invoke, the request to the BasicMath Web Service is sent. The Web Service returns the result to the browser as XML like this:

 

4

 

Now that we have tested and verified that our Web Service is functioning correctly, it is time to consume the service from a client application. There are a few things you will need to do. For a client application to consume our BasicMath Web Service, it has to know how to call it. Fortunately, a standard language for describing Web Services exists: Web Service Description Language (WSDL). WSDL describes a Web Service and the methods it exposes. You can view a Web Service s WSDL document by typing in the location of the service followed by ?wsdl, like this http://beta2eraserver.net/ServiceStation/BasicMath.asmx?wsdl. The results are shown in FIGURE 5.

 


FIGURE 5: WSDL document for the BasicMath Web Service.

 

Understanding the specifics of the WSDL document is not important at this stage. However, you will need to use the WSDL document to create a proxy class for the Web Service with the wsdl.exe tool. (This command line utility is part of the .NET Framework.) This local proxy class will reroute calls to the corresponding methods of the Web Service. The wsdl.exe tool only requires one parameter, which is either the Uniform Resource Identifier (URI) of the WSDL or the path to a downloaded WSDL document. The following command line statement calls wsdl.exe with the /l option to specify the language of the generated proxy class (in this case, C#), the /out option to specify the location and the name of the output proxy class, and the /n option to specify the namespace for the proxy class:

 

wsdl

http://beta2.eraserver.net/servicestation/BasicMath.asmx?WSDL

/l:cs /out:bin\BasicMath.cs /n:ServiceStation

 

(You will need to enter this statement on one line.) To use the generated proxy class, you will need to compile it. Because we created a C# proxy, we will need to compile it with the C# compiler:

 

csc /t:library /out:BasicMath.dll BasicMath.cs

 

After compiling the proxy class, you can reference it just as you would any other assembly. Let s create a simple Web Form that will access the BasicMath Web Service residing at EraServer.NET. Begin by creating a Web Form that contains two TextBox Web controls, one for each argument to the Add and Subtract methods. Add a Button control and a RadioButtonList control with two options, Add and Subtract to distinguish between the Add and Subtract methods. The Web Form should look like the one shown in FIGURE 6.

 


FIGURE 6: Local Web Form accessing the BasicMath Web Service.

 

With the basic Web Form constructed, you need to add the logic to access the Web Service through the proxy class. This process takes a few steps, beginning with importing the namespace of the proxy class. When you generated the proxy class, you included the /n option, which specified the namespace as ServiceStation. By importing the ServiceStation namespace, you can access the BasicMath proxy class. You import the ServiceStation namespace by adding this statement to the top of the Web Form after the Page directive:

 

<% @Import Namespace="ServiceStation" %>

 

After the namespace has been imported, you can create an instance of the proxy class using this statement:

 

Dim BMath As BasicMath = New BasicMath

 

Now that you have an instance of the proxy class, you can call the Add or Subtract methods. As an example, you can call the Add method using the statement:

 

BMath.Add(3,6)

 

Putting all these steps together, you can finish the implementation of our Web Form, which is shown in Listing One.

 

Conclusion

In this article, we looked at how Web Services can help in the area of code reuse. We also learned how to develop and consume Web Services. However, are Web Services really as great as they sound? In the next Service Station installment we will cover the pros and cons of Web Services, how practical Web Services are, and any changes we need to make in the way we work to accommodate Web Services.

 

The files referenced in this article are available for download.

 

Robert Lair is the president/CEO of Intensity Software Inc., http://www.intensitysoftware.com, a software development company specializing in Microsoft .NET development. He has been working with Microsoft .NET since early in 2001 when he worked on the IBuySpy demo application. Robert also runs the development resource sites http://www.practicalasp.net and http://www.asppages.com. You can reach Robert with questions at mailto:[email protected] or by visiting his personal Web site at http://www.robertlair.com.

 

Begin Listing One Implementation of local Web form accessing BasicMath Web Service

<% @Page Language="VB" Debug="true" %>

<% @Import Namespace="ServiceStation" %>

 

 

   Local Web Form Accessing BasicMath Web </p> <p>   Service

 

Local Web Form Accessing BasicMath Web Service

   

      

      

   

   

      

      

   

   

      

      

   

   

      

      

   

   

      

      

   

         Argument One:

      

         

      

         Argument Two:

      

         

      

         Operation:

      

         

            Add

            Subtract

         

      

          

      

         

          Text="Execute" OnClick="ExecuteOp" />

      

          

      

         Result:

         

      

End Listing One

 

Creating Web Services Using Visual Studio .NET

What if you are using Visual Studio .NET to create a Web Service? Things are a little different, because Visual Studio handles some of the details that you otherwise have to perform manually. The basic ideas, however, are the same.

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish