Vishwamohan

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

Implementing WCF Service Part 1 - Service

As you know, the New Windows Communication Services (WCF) will replace the existing ASP.NET ASMX Web Services. So, it will be wise to plan it from today as how to transit in this new service model era. I first tried experimenting with WCF Service almost a year ago with the release of .NET 3.0 but VS 2005 and VISTA did work happily on my machine but now I like VS 2008. I revisited WCF again with the release of .NET 3.5, all the sample available on the web are either old or do not give enough information if you are looking for information as how to use this service in different way over different protocol, which can closely mimic a small real word example . So, I had to spend hours trying to find solutions and put the pieces together. So, I finally thought to create an example to help others who are facing the same situation. I have tried to closely mimic the ASMX Web Service uses, which I posted in my earlier post because most of us will be planning to convert from existing old one. With all the experiments I would give one key Mantra for WCF, dig the Config file specially to understand how to configure BEB (Binding, Endpoint and Behavior). This example deals with CRUD operation on Customer data which I used in earlier post. For the simplicity I am using XML file for the data source and will consume this service with WS* Standard, SOAP based XML POST, Plain Old XML (POX) and HTTP GET and POST. I believe these are the common uses along with TCP, named pipe and MSMQ.

As you know, the New Windows Communication Services (WCF) will replace the existing ASP.NET ASMX Web Services. So, it will be wise to plan it from today as how to transit in this new service model era. I first tried experimenting with WCF Service almost a year ago with the release of .NET 3.0 but VS 2005 and VISTA did work happily on my machine but now I like VS 2008.  I revisited WCF again with the release of .NET 3.5, all the sample available on the web are either old or do not give enough information if you are looking for information as how to use this service in different way over different protocol, which can closely mimic a small real word example .  So, I had to spend hours trying to find solutions and put the pieces together. So, I finally thought to create an example to help others who are facing the same situation. I have tried to closely mimic the ASMX Web Service uses, which I posted in my earlier post because most of us will be planning to convert from existing old one. With  all the experiments I would give one key Mantra for WCF, dig the Config file specially to understand how to configure BEB (Binding, Endpoint and Behavior). This example deals with CRUD operation on Customer data which I used in earlier post. For the simplicity I am using XML file for the data source and will consume this service with WS* Standard, SOAP based XML POST, Plain Old XML (POX) and HTTP GET and POST. I believe these are the common uses along with TCP, named pipe and MSMQ.

I have created a WCF Service Project Vishwa.Example.WcfService in Visual Studio Version: 2008 with NET 3.5. This project contains following main files.
1.       Web.config – Config file ( you have always pay the highest attention here for making sure the right configuration)
2.       Customer.vb – Customer class – DataContract and Data Members ( you can compare the same exact file with ASMX Web Service)
3.       ServiceHelper.VB – Collection of CRUD Methods –exactly the same file I used in ASMX Web Service
4.       ICustomerService.vb – Interface Class – Service Contracts
5.       CustomerService.svc – WCF Customer Service Page
6.       CustomerService.svc.vb – Code behind file for the WCF Customer Service Page
 
Steps – Create a WCF Service Application and Rename the default files as mentioned above for Service1.scv or just follow the code. Next ..
Web.Config : Make sure the following Service section is correct in web.config
<services>
      <service  name="Vishwa.Example.WcfService.CustomerService" 
                                    behaviorConfiguration="Vishwa.Example.WcfService.CustomerServiceBehavior">
            <endpointaddress=""  binding="wsHttpBinding"  contract="Vishwa.Example.WcfService.ICustomerService" 
                                   bindingNamespace="http://wcfservices.vishwamohan.net/ws">
                  <identity>
                        <dnsvalue="localhost"/>
                  </identity>
            </endpoint>
            <endpointaddress="mex"binding="mexHttpBinding"   contract="IMetadataExchange" 
                                       bindingNamespace="http://wcfservices.vishwamohan.net/mex"/>
      </service>
</services>
Customer.vb: Add this class with following code. This class is a Data Transfer Object which defines the Data Contract and Data Members.
Namespace Example.WcfService
    <DataContract(Name:="Customer", Namespace:="http://schemas.vishwamohan.net/2008/01/Customer")> _
    Public Class Customer
 
        <DataMember(Name:="ID", Order:=0)> _
        Public CustID As Integer = 0
 
        <DataMember(Name:="Name", Order:=1)> _
        Public CustName As String = String.Empty
 
        <DataMember(Name:="DOB", Order:=2)> _
        Public CustDOB As DateTime = DateTime.MinValue
 
        <DataMember(Name:="Address", Order:=3)> _
        Public CustAddress As String = String.Empty
 
        <DataMember(Name:="DateCreated", Order:=4)> _
        Public DateCreated As DateTime = DateTime.Now
 
        <DataMember(Name:="DateModified", Order:=5)> _
        Public DateModified As DateTime = DateTime.Now
 
        Public Sub New()
 
        End Sub
 
    End Class
End Namespace
 
ServiceHelper.vb: Add following code. Please note that I am using a Customer Business Object which was created in my earlier post. This class is exactly same which I used in ASMX Web Service.
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.
Loading