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
ICustomerService.vb – Add following code which defines a Service Contract
Namespace Example.WcfService
<ServiceContract(Name:="ICustomerService", NameSpace:="http://wcfservices.vishwamohan.net")> _
Public Interface ICustomerService
<OperationContract(Name:="GetCustomer")> _
Function GetCustomer(ByVal ID As Integer) As Customer
<OperationContract(Name:="GetCustomers")> _
Function GetCustomers() As List(Of Customer)
<OperationContract(Name:="AddCustomer")> _
Function AddCustomer(ByVal CustomerRecord As Customer) As Integer
<OperationContract(Name:="UpdateCustomer")> _
Function UpdateCustomer(ByVal CustomerRecord As Customer) As Boolean
<OperationContract(Name:="DeleteCustomer")> _
Function DeleteCustomer(ByVal ID As Integer) As Boolean
End Interface
End Namespace
CustomerService.svc –Make sure you have following code as the markup in the service page
<%@ ServiceHost Language="VB" Debug="true" Service="Vishwa.Example.WcfService.CustomerService" CodeBehind="CustomerService.svc.vb" %>
CustomerService.svc .vb – Add following code for this service
Namespace Example.WcfService
<ServiceBehavior(Name:="CustomerService", NameSpace:="http://wcfservices.vishwamohan.net")> _
Public Class CustomerService
Implements ICustomerService
Public Sub New()
End Sub
Public Function GetCustomer(ByVal ID As Integer) As Customer Implements ICustomerService.GetCustomer
Return ServiceHelper.GetCustomerData(ID)
End Function
Public Function GetCustomers() As List(Of Customer) Implements ICustomerService.GetCustomers
Return ServiceHelper.GetCustomersData()
End Function
Public Function AddCustomer(ByVal CustomerRecord As Customer) As Integer Implements ICustomerService.AddCustomer
Return ServiceHelper.AddCustomerData(CustomerRecord)
End Function
Public Function DeleteCustomer(ByVal ID As Integer) As Boolean Implements ICustomerService.DeleteCustomer
Return ServiceHelper.DeleteCustomerData(ID)
End Function
Public Function UpdateCustomer(ByVal CustomerRecord As Customer) As Boolean Implements ICustomerService.UpdateCustomer
Return ServiceHelper.UpdateCustomerData(CustomerRecord)
End Function
End Class
End Namespace
That’s all, you are done. Now, compile the project and make sure that you see the service page. It will look like as
This service is ready to be hosted in IIS, which I believe is the easiest way to put the example together. So put it under a virtual directly let’s say ExampleService under IIS. In next post I will consume this service on WS* Http Binding. Till now, you might have created a test service and used it anyway, so it might be sounding easy, but as I will move to different bindings, and how to consume that will be interesting. For each type I will make a new post to keep it clear and easy.