|
XML Web Services
Sample Service 1
In this section we will create a simple Web service. When working with Web services
the namespaces that are required are summarized as follows:
System.Web.Services: Namespace consists a minimal and complete
set of types needed to build a Web service
System.Web.Services.Description: This allows us to interact
with WSDL programmatically
System.Web.Services.Discovery: These types allow a consumer
to discover the Web services installed on a given machine
System.Web.Services.Protocols: This namespace defines a
number of types that represents invocation protocols (HTTP-GET, HTTP-POST and SOAP)
The System.Web.Services namespace
The System.Web.Services namespace is the namespace that we normally use in most of
the projects as the types we need are already defined in this namespace. Following
are the members of the System.Web.Services namespace:
WebMethodAttribute: Adding a <WebMethod()> attribute
to a method in a Web service makes the method callable from a remote client through
HTTP. This attribute exposes the functionality of the method to which it is applied
to the outside world.
WebService: This defines the optional base class for Web
Services.
WebServiceAttribute: The WebService attribute can be used
to add information to a Web service that can describe it's functionality.
WebServiceBindingAttribute: Declares a binding protocol
a given Web service method is implementing.
Coding a Sample Service
We will now create a sample service. This a simple service that converts a given distance
from Kilometers to Miles and vice versa. Start Visual Studio .NET and open a new project
from File->New-> Project. In the Projects Type pane select Visual Basic Projects
and in the templates select ASP.NET Web Service, name this
service as ConvertUnits and click OK. The new project dialog looks like the image
below.
By default, Web service projects automatically create a new virtual directory under
IIS and will store our files there. Switch to code view of the Web service to take
you to the code behind file which is a file with .asmx.vb extension. If you notice
the Solution Explorer window you will find four files which are the Global.asax, Service1.asmx,
ConvertUnits.vsdisco and the Web.config file. The Global.asax file allows us to respond
to
global-level events, the Web.config file allows us to declaratively configure our
new Web service, the .asmx file is a Web service file that define the methods of the
service and the .vsdisco file is a Discovery file that contains an XML description
of the Web services at a given URL.
By default the code behind file looks like this when you open it:
Imports System.Web.Services
<WebService(Namespace := "http://tempuri.org/")> _
Public Class Service2
Inherits System.Web.Services.WebService
#Region " Web Services Designer Generated Code "
' WEB SERVICE EXAMPLE
' The HelloWorld() example service returns the string Hello World.
' To build, uncomment the following lines then save and build the project.
' To test this web service, ensure that the .asmx file is the start page
' and press F5.
''<WebMethod()> Public Function HelloWorld() As String
' HelloWorld = "Hello World"
' End Function
End Class
|
We will build on the above mentioned code behind file. We will implement some simple
functionality adding our own methods. The service which we will build will convert
distance expressed in Kilometers to Miles and vice versa. The code for that looks
like this:
Imports System
Imports System.Web.Services
<WebService(Namespace := "http://tempuri.org/")> _
Public Class Service1 Inherits System.Web.Services.WebService
#Region " Web Services Designer Generated Code "
#End Region
<WebMethod()> Public Function ConvertUnits(ByVal EnterUnit As Decimal, _
ByVal FromUnits As String, ByVal ToUnits As String)
'ConvertUnits function with three arguments
Select Case FromUnits.ToUpper.Chars(0)
'making a selection with Select Case
Case "K"
'for converting distance from kilometers to miles
Select Case ToUnits.ToUpper.Chars(0)
Case "K"
Return EnterUnit
'if both FromUnits and ToUnits are same, returns the entered distance
Case "M"
Return EnterUnit / 1.4
'converts distance from kilometers to miles, assuming 1 mile=1.4 kilometer
Case Else
'to throw exception
End Select
Case "M"
'for converting distance from miles to kilometers
Select Case ToUnits.ToUpper.Chars(0)
Case "M"
Return EnterUnit
Case "K"
Return EnterUnit * 1.4
'converts distance from miles to kilometers
Case Else
'to throw exception
End Select
End Select
End Function
End Class
|
After finishing with the code run it by selecting Debug->Start from
the main menu or by pressing F5 on the Keyboard. By default our browser functions
as a makeshift client and shows an HTML view of the methods market with the <WebMethod()>
attribute. Click here to view the page
that first loads when you run this service. Click on the link ConvertUnits. Clicking
on the link takes you to a page that provides Textbox types that allow us to
enter some values in them. Enter some value in the EnterUnit field and in the FromUnits
field enter either M or K and in the ToUnits filed enter K or M. If you wish to convert
1000 Kilometers into Miles then you need to enter 1000 in the EnterUnit field, K in
the FromUnits and M in the ToUnits. Once you are done with it, click invoke. This
will invoke the method we wrote in code and the result will be returned via an XML
attribute. Click here to run the service
now. That's all it takes to create a simple Web service.
|