Vishwamohan

Welcome to Vishwa's blog - Technology, Spirituality and More...

Implementing ASMX Web Service Part 1 - Service

You must be wondering, that in WCF era ASMX Web Service! I wrote the first one 5 years ago and now trying to move them to WCF Service, but lot of companies have not even adopted the web service and some have only started adopting it recently, and now they realized that a new era has come. I will discuss my experiments & experiences with WCF in later posts. However, I would like to post a simple Customer ASMX Web Service and then consume it by a ASP.NET Web Page via Server Side and then Client Side using direct SOAP based XML through HTTP Post. Later, I will create a similar WCF Service with exactly same methods and will access the data exactly same way. Remember this is not a simple HelloWorld Example, this Web Service deals with primitive data type as well as complex one. How far you would like to dive deep into it, will be up to you.

          In order to keep the example sweet and simple and close to real world, I will be using a Customers XML data file and will perform CRUD operations.  I created this project using Visual Studio 2008 and wrote code using VB.NET.

           You can also use Visual Studio 2005 for this project.

This project basically contains 5 file, you can create them in following order.
1.       Web.Config
2.       Customer.vb - Customer class
3.       ServiceHelper.VB – Collection of CRUD Methods
4.       CustomerWebService.asmx – ASMX Web Service Page
5.       CustomerWebService.asmx.vb – Code behind file of the .asmx file
 
My Project Name is – Vishwa.Example.WebService
 
Web.Config
If you are planning to deploy this web service for HTTP GET and POST uses, add the following lines under <system.web> section. By default they will only work if you are running locally.
<webServices>
           <protocols>
                     <add name="HttpPost" />
                     <add name="HttpGet" />
            </protocols>
</webServices>
 
Customer.vb - Add this class in your project with following code. This class is a Data Transfer Object.
Namespace Example.WebService
    Public Class Customer
 
        <System.Xml.Serialization.XmlElement(ElementName:="ID", Order:=0)> _
        Public CustID As Integer = 0
 
        <System.Xml.Serialization.XmlElement(ElementName:="Name", Order:=1)> _
        Public CustName As String = String.Empty
 
        <System.Xml.Serialization.XmlElement(ElementName:="DOB", Order:=2)> _
        Public CustDOB As DateTime = DateTime.MinValue
 
        <System.Xml.Serialization.XmlElement(ElementName:="Address", Order:=3)> _
        Public CustAddress As String = String.Empty
 
        <System.Xml.Serialization.XmlElement(ElementName:="DateCreated", Order:=4)> _
        Public DateCreated As DateTime = DateTime.Now
 
 
        <System.Xml.Serialization.XmlElement(ElementName:="DateModified", Order:=5)> _
        Public DateModified As DateTime = DateTime.Now
 
        Public Sub New()
 
        End Sub
 
    End Class
End Namespace
ServiceHelper.vb - Add this class now with following code. Please note that I am using a Customer Business Object, which I created in my earlier post.
 
Imports Vishwa.Example.Business
 
Namespace Example.WebService
    Public NotInheritable Class ServiceHelper
 
        Private Shared instance As New ServiceHelper
        Private Shared  bizObj As New CustomerBiz
        Private Sub New()
 
        EndSub
 
        Friend Shared Function GetCustomerData(ByVal ID AsInteger) As Customer
            Dim bizCustomer As CustomerBiz = Nothing
            bizCustomer = bizObj.GetCustomer(ID)
            Return GetCustomerDataFromBizCustomer(bizCustomer)
        End Function
 
        Friend Shared Function GetCustomersData() As List(Of Customer)
            Dim customers AsNew List(Of Customer)
            Dim bizCustomers AsNew List(Of CustomerBiz)
            bizCustomers = bizObj.GetCustomers()
 
            ForEach bizCustRec As CustomerBiz In bizCustomers
                customers.Add(GetCustomerDataFromBizCustomer(bizCustRec))
            Next
            Return customers
 
        End Function
 
        Friend Shared Function AddCustomerData(ByVal custRecord As Customer) As Integer
            Return bizObj.AddCustomer(custRecord.CustName, custRecord.CustDOB, custRecord.CustAddress)
        End Function
 
        Friend Shared Function UpdateCustomerData(ByVal custRecord As Customer) As Boolean
            Return bizObj.UpdateCustomer(custRecord.CustID, custRecord.CustName, custRecord.CustDOB, custRecord.CustAddress)
        End Function
 
        Friend Shared Function DeleteCustomerData(ByVal ID AsInteger) As Boolean
            Return bizObj.DeleteCustomer(ID)
        End Function
 
        Private Shared Function GetCustomerDataFromBizCustomer(ByVal bizCust As CustomerBiz) As Customer
            Dim custRecord AsNew Customer
            If Not bizCust Is Nothing AndAlso bizCust.CustID > 0 Then
                custRecord.CustID = bizCust.CustID
                custRecord.CustName = bizCust.CustName
                custRecord.CustDOB = bizCust.CustDOB
                custRecord.CustAddress = bizCust.CustAddress
                custRecord.DateCreated = bizCust.DateCreated
                custRecord.DateModified = bizCust.DateModified
            End If
            Return custRecord
        End Function
 
    End Class
End Namespace
 CustomerWebService.asmx – Add this ASMX file, a Code behind will be added by default. The markup inside CustomerWebService.asmx will looks like
<%@ WebService Language="VB" CodeBehind="CustomerWebService.asmx.vb" Class="Vishwa.Example.WebService.CustomerWebService" %>
  
 
CustomerWebService.asmx.vb Add following code in this file
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
 
Namespace Example.WebService
    <System.Web.Script.Services.ScriptService()> _
    <System.Web.Services.WebService(Name:="CustomerWebService", Namespace:="http://webservices.vishwamohan.net", _
                                    Description:="Customer ASMX Web Service.")> _
    <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <ToolboxItem(False)> _
    Public Class CustomerWebService
        Inherits System.Web.Services.WebService
 
        <WebMethod(Description:="Gets a customer record on customer ID.")> _
        Public Function GetCustomer(ByVal ID As Integer) As Customer
            Return ServiceHelper.GetCustomerData(ID)
        End Function
 
        <WebMethod(Description:="Gets all customer record.")> _
        Public Function GetCustomers() As List(Of Customer)
            Return ServiceHelper.GetCustomersData()
        End Function
 
        <WebMethod(Description:="Adds a customer record and returns customer ID.")> _
        Public Function AddCustomer(ByVal CustomerRecord As Customer) As Integer
            Return ServiceHelper.AddCustomerData(CustomerRecord)
        End Function
 
        <WebMethod(Description:="Deletes a customer record on customer ID.")> _
        Public Function DeleteCustomer(ByVal ID As Integer) As Boolean
            Return ServiceHelper.DeleteCustomerData(ID)
        End Function
 
        <WebMethod(Description:="Updates a customer record.")> _
        Public Function UpdateCustomer(ByVal CustomerRecord As Customer) As Boolean
            Return ServiceHelper.UpdateCustomerData(CustomerRecord)
        End Function
    End Class
End Namespace
You are done!
Now you compile the project and run. You should be able to see following 5 web methods.
In next post I will consume these methods through a web page via direct web service integration in project as well as through SOAP XML HTTP Post.

Comments (1) -

  • srinivas

    9/10/2008 6:26:39 PM | Reply

    Excellent article!!

Loading