Thursday, February 7, 2013

Web Services


Written by Gary Rasmussen,
Developer at Rockton Software
What are Web Services?
Wikipedia defines a web Service as a method of communication between two electronic devices over the World Wide Web.  In the traditional model, you would write a 2 tier application where you would write the server side component and the client side component to be distributed as a single product to be installed by the customer.  When you need to make an update to your product, the update has to be rolled out to each and every customer where they need to update their individual product in their environment.

There are a couple of benefits to using Web Services rather than the traditional model, but the single most important benefit is interoperability.  They allow the developer the freedom to write code in their favorite programming language regardless of platform  while at the same time making it easier to distribute enhancements and bug fixes because the updates  only have to happen in one place and all clients are therefore theoretically all updated as well at the same time.

For this example, we will create 3 files that will allow us to convert between Fahrenheit and Celsius.  The files are:

  • The class file: this file contains the web methods you want to expose.
  • The asmx file: this is the file your client application will point to in order to call the web methods.
  • The client file: this file contains code that will consume the Web Service making it possible to call the web methods.

In Visual Studio, create a new website and add a new web service item to it.  Call it “Conversions.”  This will automatically create the Conversions.asmx file with a single line that points to a Conversions class file in the App_Code folder of your website for you.

Conversions.asmx

 
App_Code/Conversions.cs

 
This Web Service exposes two web methods for converting between Fahrenheit and Celsius.  At this point you are done with your web service project.  Build the project and close.

Next, you need to expose the Web Service so your client application will be able to see it.  To keep things simple for this example, just create a new website in IIS on your local machine and point it to the folder that contains the website that you just created.

Your client application can be pretty much anything you want as long as it is able to consume a Web Service.  For this example I am going to create a simple console application using Visual Studio and I will name it “MyWebServiceTester”.

Next we need to add a reference to our Web Service.  Right-mouse click the “MyWebServiceTester” project in your console application and click “Add Service Reference.”  Click “Advanced,” and then “Add Web Reference.” In the URL textbox at the top, enter the path to your asmx file then click the green arrow.  Once it resolves, give the web reference a name and click “Add Reference.”


Now that we have a reference to our Web Service, we can write some code to test it.  Open the Program.cs file inside your console application and add the following code to the Main.

App_Code/Conversions.cs



Running your console application should yield the following results.



I hope this basic example gives you the information you need to start writing your own Web Services.


Want more information about Rockton Software? Head to our website www.rocktonsoftware.com!

& don't forget to follow Rockton Software on twitter!

No comments:

Post a Comment