Enterprise Software

Consume a Web service in a .NET app

Consuming a Web service in a .NET application is a straightforward process. Tony Patton discusses a Web services project with Java on one side and .NET on the other.

I once worked on a project that utilized a backend built with Java and a user interface constructed with .NET. Communication between the Java and .NET components was necessary for a successful project, so a Web service was installed on the Java side with it being utilized by the .NET front-end.

The Web service eased the process of integrating the two disparate environments, so it was a win-win for both sides. Also, I was quite impressed with how easy it was to utilize the external Web service on the .NET side.

What you need

Using a Web service in a .NET project involves creating and compiling a proxy class, which is generated from a reference to the Web service. This reference may be in several formats:

  • ASMX file: A Web service created with the .NET Framework
  • WSDL file: A file or URL that includes the Web Services Description Language (WSDL), which describes the service and its interface
  • DISCO: A Microsoft technology for publishing and discovering Web services (It makes it possible for clients to reflect against endpoints to discover services and their associated WSDL documents.)

If you use Visual Studio .NET, it's easy to add a Web service to your project by adding a Web Reference. This allows you to enter the Web service's location (using one of the methods from the list). In addition, you may use the WSDL.exe tool included with the .NET Framework installation.

Here's the process of using a Web service:

  1. Locate the Web service with a file or URL.
  2. Create the Web service's proxy class using WSDL.exe or your favorite IDE.
  3. Compile the proxy class using the appropriate compiler (or IDE).
  4. Use the proxy class in code to take advantage of the Web service.

For demonstration purposes, I'll use a sample Web service from the xMethods' Web site. This service allows a temperature to be retrieved given a proper U.S. zip code. At this point, step one is completed so the WSDL file is used to create the proxy class:

WSDL.exe /out:TemperatureService.cs /n:BuilderWS /l:CS
http://www.xmethods.net/sd/2001/TemperatureService.wsdl

The VB.NET equivalent requires one change: The language option should be /l:VB. The result of this step is a source code file.

Click here to see the C# equivalent.

Now you can compile the source code with your IDE or via the command line. The following line shows how the code may compile with the command-line C# compiler:

csc /t:library /out:TemperatureService.dll TemperatureService.cs
/reference:System.Web.Services.dll /optimize

A few points on the compilation:

  • The /t:library option tells the compiler to create a .dll file as opposed to an executable (.exe).
  • The /out directive tells the compiler where to store the results.
  • The /optimize option creates the smallest and fastest code.
  • The /reference directive tells the compiler which .NET namespaces are used in the code. In the VB.NET example that follows, all necessary namespaces are included.

In this example, we include the System.Web.Services namespace via its .dll.

If your language of choice is VB.NET, you can easily use the VB.NET compiler. Click here.

The result of these steps is a .dll that may be used in a project to call the Web service.

Using the Web service

At this point, you can use the Web service in a project. You can place the new .dll in the same directory as your project or install it in the Global Assembly Cache (GAC). In the next example, the service is utilized in a simple ASP.NET page via C#:

<%@ Page language="c#" %>
<%@ Import Namespace="BuilderWS" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<script language="C#" runat="server">
private void Page_Load(object sender, System.EventArgs e) {
TemperatureService testService = new TemperatureService();
float temp = testService.getTemp("40243");
this.txtTemp.Text = temp.ToString();
}
</script>
<html><head>
<title>Builder.com Web Service utilization example</title>
</head>
<body>
<form id="frmWSExample" method="post" runat="server">
<asp:TextBox id="txtTemp" style="Z-INDEX: 101; LEFT: 224px; POSITION: absolute;
TOP: 64px" runat="server" />
<asp:Label id="lblOutput" style="Z-INDEX: 102; LEFT: 32px; POSITION: absolute;
TOP: 64px" runat="server" Width="176px">The current temperature is:</asp:Label>
</form></body></html>

The Web service accepts the zip code parameter as a string value, while returning a float value representing the temperature. The zip code passed is hard coded, but it could easily be dynamic, allowing the user to specify the zip. The VB.NET equivalent follows:

<%@ Page language="vb" %>
<%@ Import Namespace="BuilderWS" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<script language="vb" runat="server">
Sub Page_Load
Dim testService As New TemperatureService()
Dim temp As double
temp = testService.getTemp("40243")
me.txtTemp.Text = temp.ToString()
End Sub
</script>
<html><head>
<title>Builder.com Web Service utilization example</title>
</head><body>
<form id="frmWSExample" method="post" runat="server">
<asp:TextBox id="txtTemp" style="Z-INDEX: 101; LEFT: 224px; POSITION: absolute;
 TOP: 64px" runat="server" />
<asp:Label id="lblOutput" style="Z-INDEX: 102; LEFT: 32px; POSITION: absolute;
 TOP: 64px" runat="server" Width="176px">The current temperature
is:</asp:Label>
</form></body></html>

Although this listing uses VB.NET, the Web service it calls could easily be in C# or any other .NET-supported language, since you're utilizing the .dll file.

A straightforward process

Consuming a Web service in a .NET application is a straightforward process. As Web services continue to evolve and develop, you may find a need for them in your next project.

While I hate to use a technology simply for the sake of using it, this project with Java on one side and .NET on the other provided a situation in which a Web service was the perfect fit.

TechRepublic's free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET. Automatically sign up today!

About

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

0 comments