Call a Web Service
Web services provide a way to connect systems with little connectivity.
There are two types of services which you can consume in your web applications:
- ASP.NET Web Services are bound to IIS and ASP.NET
- WCF Services haven’t got these limitations
We will cover both of them. Let’s start with the former!
ASP.NET Web Service (aka .asmx files) are simple wrapper classes around standard SOAP services. They let you focus on what you want to expose instead of the hows and whys. To create one, you should write a class that inherits from System.Web.Services.WebService, or simply click add new item, then select the Web Service file. This will generate the following output:
Our WebService class inherits from WebService, and has a method (with an attribute called WebMethod), that returns the standard hello world string.
Before we look at the insides, here’s a brief note: web services are expensive in every manner, so use them wisely. The best practice is to create a few services which handles a lot of work, and never to create a lot of services whit relatively small amount of work to do.
To return to the topic, the code above isn’t the only thing generated for your web service. In fact, there’s a .asmx file, which serves as a pointer to your code, with the following contents:
<%@ WebService Language=”C#” CodeBehind=”~/App_Code/WebService.cs” Class=”WebService” %>
I think no further explanation is needed for this piece of code.
Now let’s take a look at the code above. It starts with a WebServiceAttriubte. In our code, it also includes a namespace declaration. This is exactly what this class does. Provides information about your service. You can omit it, too, because it isn’t necessary for services.
All ASP.NET Web Services should inherit from the WebService class.This class providers access to the ASP.NET intrinsic objects such as Application or Session. You can omit this one too, if you aren’t require the access for the intrinsic objects.
What can’t be omitted from a web service is the WebMethodAttribute. You must have at least a single public method with the WebMethodAttribute, to expose it. You can set a few thing in the constructor, such as:
- enableSessionState: does what its name says.
- cacheDuration: the timespan in seconds for caching the result of the service.
Note that to expose classes with a Web Service, the classes must be serializable to XML format. When you’re working with your custom classes, you have two options: create them inside a Web Service Application, or mark them as Serializable.
The consuming of an ASP.NET Web Service is easy. Just simply right click on your project file, and select Set Web Reference. You will then need to type in a local (or global) publically accessible web site which provides web services (.asmx files). Visual Studio will take care of the rest. It generates a proxy class and places it into the App_WebReferences directory. You can then write code against the service method just like any other code in your application.
Here I must mention the WSDL.exe tool. This application creates proxy classes from existing web services. Simply specify an URL as a parameter, and the tool generates the proxy class of the service hosted there in C#. Use the language switch to chose another language, or the out switch to specify the output path.