Article

.NET Web Services Made Easy

Page: 1 2 3 4 5 6 Next

Data Types

Like the functions you create for your site, Web Services accept and return a variety of data types:

  • String
  • Int
  • Boolean
  • Byte
  • Double
  • Datatype
  • Enumeration
  • Float
  • Long
  • Short
  • Unsignedbyte
  • Unsignedint
  • Unsignedlong
  • Unsignedshort

And, as with normal classes and methods, you decide what data types can be accepted. However, the types that you can accept will be heavily influenced by what protocol is used to consume the Service. SOAP is the most extensible because it's an XML-based protocol, but because of this it also uses the most bandwidth. Http-Get and Http-Post on the other hand, being name/value pairs, use less bandwidth, but are also more limited.

Our First Simple Web Service

Let's create a Web Service! We're going to build a simple script that returns a string -- something nice and simple to start with. After that, we'll move onto bigger and brighter things!

Creating the Web Method

If you've worked with .NET before, and you've created a custom class, then you're not that far from creating a Web Method! There isn't that much difference between the two really: exposed public methods are available through the Web Service, much like the public methods of a class are available to the rest of the application.

Ok, so open your favorite text editor or IDE, and save a blank document as webservice1.asmx. No, I didn't mistype the extension. .NET Web Services files are saved as .asmx.

Ok, so you've waited until now to get your hands on some code, so here you are:

<%@ WebService Language="VB" Class="SitePoint" %>  
 
Imports System.Web.Services  
 
<WebService(NameSpace:="http://www.sitepoint.com/")>  
Public Class SitePoint  
 
 <WebMethod(Description:="It say hello!!")> Public  
 Function returnHello() As String  
 Return "HELLO TO THE BIG WIDE WORLD!"  
 End Function  
End Class

Look a little familiar? Thought so! Let's go over it.

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

The first line looks a little like the normal Page directive, but instead of Page, it's a WebService directive, which tells the .NET Framework that this class is to be exposed as a Web Service.

Imports System.Web.Services

Next we import the System.Web.Services Namespace so we can access .NET Web Services classes.

<WebService(NameSpace="http://www.sitepoint.com/")>  
Public Class SitePoint

This line provides an XML Namespace for the Web Service. Now, if we'd left this line out (it's optional) it would have bee assigned the Namespace<http://tempuri.org>. It's generally a good idea to supply your own Namespace, as this helps to distinguish yours from other Web Services.

<WebMethod(Description:="It say hello!!")>  
Public Function returnHello() As String

To create a Web Service, you have to expose part of it. You can do this by using the WebMethod attribute, and by declaring the class and methods as public. In the above line, we declare the returnHello function as a WebMethod and public, which means that it will be available as a service.

     Return "HELLO TO THE BIG WIDE WORLD!"  
  End Function  
End Class

With these lines, we return a simple string, and close the function and class. Now, navigate to the webService1.asmx file and you'll see that the .NET Framework has automatically rendered it.

993_ws1

Cool, eh? As you can see, the page displays the name of the Web Service at the top of the page and then lists the methods that have been made available, using the WebMethod attribute. Now click the name of a WebMethod, in this case the returnHello method, and a page describing the Web Service will appear.

You'll see the method name, along with the description we set in the code. Below this you'll find a header called Test and a button with Invoke written on it. Don't hit it yet, we'll come back to this in a moment!

Below this, you'll see some sample SOAP, Http-Get and Http-Post requests and responses. They should look a little familiar!

Now hit the Invoke button! A window should pop up that looks something like this:

993_ws2

This is how our exposed service will appear.

Ok, now back to the first page. There's something we never covered, anyone notice what it was?

993_ws3

This link will open the Service Description, funnily enough, which is called the WSDL document. If you read Kev's article, congratulations - you know what I'm talking about! If not, shame on you!

Basically, the WSDL (Web Service Description Language) defines the Web Service and how it will be consumed. There are sections that detail how to interact with it using SOAP, Http-Get and Http-Post. Not bad, eh? As you can see, the .NET Framework makes creating Web Services easier than cooking toast!

Ok, we've built a simple Web Service that returned a string. Let's move onto a Web Services that you can actually interact with!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links