Consume a Web service in a .NET app - Java - Techguide

Consume a Web service in a .NET app

 

Summary

Consuming a Web service in a .NET application is a straightforward process. Tony Patton discusses a Web services project with both Java and .NET.

Events

Echelon 2012
June 11 and 12, 2012

University Cultural Centre, National University of Singapore

Startup Asia Jakarta 2012
June 7 and 8, 2012

12th Floor, Annex Building, Wisma Nusantara Complex, Jl. M.H. Thamrin No. 59 Jakarta 10350, Indonesia

MMA Forum Singapore
April 23-25, 2012

Grand Hyatt Singapore

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.

Talkback

Add your opinion

In order to post a comment, you need to be registered. (Sign In or register below)

Post your comment

ZDNet Asia Live

42 bands from 15 countries to feature at Music Matters Live 2012 which will beam live via YouTube for 1st time this year. #mm12

Music Matters to be launched in Bali via partnership w/Telkom Indonesia. #mm12

HP to shed 27K workers by 2014 http://t.co/OevueOGh http://t.co/erFSwAUB #arcavir

http://t.co/VNaUVSe1 HP to shed 27K workers by 2014: IT vendor plans exit of 8 percent of gl... http://t.co/5LKpdBSZ http://t.co/wiqTBKkj

China solar cell makers seek Taiwan partnershipshttp://bit.ly/JErUGz via @zdnetasia #solar #energy #china

Malaysia organizations don't realize severity of cyberattacks http://t.co/PUCv68Rd

News: Radio Costa Rica by EnjoyIT 1.0: Radio Costa Rica allows you to listen to a great var... http://t.co/BLzVT5As http://t.co/1Dhcy6ki

The key for mobile operators is identifying the applications that are popular with subscribers on their network. They can then work partn...

3 hours ago by camcullen on Experience trumps content in apps monetization

Experience trumps content in apps monetization | ZDNet http://t.co/gBXcjbGd

Experience trumps content in apps monetization - ZDNet Asia News: "What we are doing currently is not to monetiz... http://t.co/S2EZtd8m

Malaysia organizations don't realize severity of cyberattacks: "Minister Maximus Johnity Ongkili said at the Sec... http://t.co/bgVlOBvx

#security Malaysia organizations don't realize severity of cyberattacks: "Minister Maximus Johnity Ongkili said ... http://t.co/hkFb4zrI

Malaysia organizations don't realize severity of cyberattacks http://t.co/EEEmRM3j via @zdnetasia

Malaysia organizations don't realize severity of cyberattacks - ZDNet Asia News http://t.co/YpNMYgb5

Malaysia organizations don't realize severity of cyberattacks http://t.co/FFems54Q

China solar cell makers seek Taiwan partnerships http://t.co/p5Hh7kJD

So much as we know , MTK6575 extremely integrated frequency1GHz ARM Cortex-A9 processor, the superiority of 3G / HSPA Modem, and help the...

1 day ago by y15822137359 on 5 SaaS adoption speed bumps to avoid

I reckon your view: "CRM is strategy, not software", if a company replicating the approach uses in ERP implementation into CRM, what they...

3 days ago by wykoong on Gartner: Mobile CRM gives better ROI than social

This video will teach you about the Excel fill handle but also provide you with a workook to download... http://www.youtube.com/watch?v=...

3 days ago by TradeBrother on A quick fill handle trick for Microsoft Excel

waiting...

5 days ago by eapete on What should count in a company's market value?

Boy, you've opened a can of worms now.

Wait for the rants & raves.

5 days ago by eapete on What should count in a company's market value?

I was puzzling before this whether to replicate the success formula we executed for a financial institute, and come out with a standard s...

6 days ago by wykoong on Drop the egos, copy ideas, then innovate