Generic Data Access Layer for WCF : Part 4

In this article, we will create a WCF Business Layer and its methods.

In Part 3: Generic Data Access Layer for WCF we saw how to build our generic entities. 
 
In this article, I will create the WCF Business Layer and add its methods. 
 
WCF Service Application
 
Let's create a new project using Visual Studio and select the WCF Service Application project type.
 
GenWCF1.gif
 
First step, I always clean the default code generated by Visual Studio and clean up our WCF service code.
 
Add Data Access Layer Reference
 
Do you remember that we created a class library in our previous articles of this series? Now, it is time to use that class library (DAL).
 
Add a reference to that class library by using Add Reference option in the Solution Explorer. Once the reference is added, let's add the System.Data.Entity Reference to the project as well.
 
GenWCF2.gif 
 
Created the Service Methods as shown below.
  1. public class Service1 : IService1  
  2. {  
  3.     public void Add(TEntity entity)  
  4.     {  
  5.         PublishingCompanyEntities context = new PublishingCompanyEntities();  
  6.         ObjectSet<TEntity> _ObjectSet = context.CreateObjectSet<TEntity>();  
  7.         _ObjectSet.AddObject(entity);  
  8.     }
  9.   
  10.     public TEntity Get(TEntity entity)  
  11.     {  
  12.         PublishingCompanyEntities context = new PublishingCompanyEntities();  
  13.         ObjectSet<TEntity> _ObjectSet = context.CreateObjectSet<TEntity>();  
  14.         TEntity company = _ObjectSet.First();  
  15.         return company;  
  16.     }  
  17. }  
The Service Contract is shown below.
  1. [ServiceContract]  
  2. public interface IService1  
  3. {  
  4.     [OperationContract]  
  5.     void Add(TEntity entity);  
  6.   
  7.     [OperationContract]  
  8.     TEntity Get(TEntity entity);  
  9. }  
Creating the Client
 
Now let's create a client application that will consume the WCF service. 
 
Create a Console application using Visual Studio 2010. 
 
GenWCF3.gif 
 
Next, add reference to the WCF service to the project so we can access the WCF service's methods.
 
GenWCF4.gif 
 
Once the service is added, let's create a Service object and call its methods.
 
The following code snippets the Service object and access articles using the GenericDal object. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Client.DataServiceReference;  
  6. using GenericDal;  
  7.   
  8. namespace Client  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {   
  14.             Service1Client client = new Service1Client();  
  15.              GenericDal.TEntity entity = new GenericDal.Article();  
  16.              client.Get(entity);  
  17.         }  
  18.     }  
  19. }  
Now build and run the project.
 
GenWCF5.gif 
 
The Run fails and we get the above error when we debug. There seems to be an error in the serialization in WCF. 
 
Let's figure out how to resolve this problem in my next article.