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.Config2. Customer.vb - Customer class3. ServiceHelper.VB – Collection of CRUD Methods4. CustomerWebService.asmx – ASMX Web Service Page5. CustomerWebService.asmx.vb – Code behind file of the .asmx fileMy Project Name is – Vishwa.Example.WebServiceWeb.ConfigIf 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.WebServicePublic 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.NowPublic Sub New()End SubEnd ClassEnd NamespaceServiceHelper.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.BusinessNamespace Example.WebServicePublic NotInheritable Class ServiceHelperPrivate Shared instance As New ServiceHelperPrivate Shared bizObj As New CustomerBizPrivate Sub New()EndSubFriend Shared Function GetCustomerData(ByVal ID AsInteger) As CustomerDim bizCustomer As CustomerBiz = NothingbizCustomer = bizObj.GetCustomer(ID)Return GetCustomerDataFromBizCustomer(bizCustomer)End FunctionFriend 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 bizCustomerscustomers.Add(GetCustomerDataFromBizCustomer(bizCustRec))NextReturn customersEnd FunctionFriend Shared Function AddCustomerData(ByVal custRecord As Customer) As IntegerReturn bizObj.AddCustomer(custRecord.CustName, custRecord.CustDOB, custRecord.CustAddress)End FunctionFriend Shared Function UpdateCustomerData(ByVal custRecord As Customer) As BooleanReturn bizObj.UpdateCustomer(custRecord.CustID, custRecord.CustName, custRecord.CustDOB, custRecord.CustAddress)End FunctionFriend Shared Function DeleteCustomerData(ByVal ID AsInteger) As BooleanReturn bizObj.DeleteCustomer(ID)End FunctionPrivate Shared Function GetCustomerDataFromBizCustomer(ByVal bizCust As CustomerBiz) As CustomerDim custRecord AsNew CustomerIf Not bizCust Is Nothing AndAlso bizCust.CustID > 0 ThencustRecord.CustID = bizCust.CustIDcustRecord.CustName = bizCust.CustNamecustRecord.CustDOB = bizCust.CustDOBcustRecord.CustAddress = bizCust.CustAddresscustRecord.DateCreated = bizCust.DateCreatedcustRecord.DateModified = bizCust.DateModifiedEnd IfReturn custRecordEnd FunctionEnd ClassEnd NamespaceCustomerWebService.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 fileImports System.Web.ServicesImports System.Web.Services.ProtocolsImports System.ComponentModelNamespace 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 CustomerWebServiceInherits System.Web.Services.WebService<WebMethod(Description:="Gets a customer record on customer ID.")> _Public Function GetCustomer(ByVal ID As Integer) As CustomerReturn 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 IntegerReturn ServiceHelper.AddCustomerData(CustomerRecord)End Function<WebMethod(Description:="Deletes a customer record on customer ID.")> _Public Function DeleteCustomer(ByVal ID As Integer) As BooleanReturn ServiceHelper.DeleteCustomerData(ID)End Function<WebMethod(Description:="Updates a customer record.")> _Public Function UpdateCustomer(ByVal CustomerRecord As Customer) As BooleanReturn ServiceHelper.UpdateCustomerData(CustomerRecord)End FunctionEnd ClassEnd NamespaceYou 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.

No comments:
Post a Comment