Generic Data Access Layer for WCF : Part 4

In the 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. 


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 add the System.Data.Entity Reference to the project as well.


Created the Service Methods as shown below.

   public class Service1 : IService1
        public void Add(TEntity entity)
            PublishingCompanyEntities context = new PublishingCompanyEntities();
            ObjectSet<TEntity> _ObjectSet = context.CreateObjectSet<TEntity>();

        public TEntity Get(TEntity entity)
                  PublishingCompanyEntities context = new PublishingCompanyEntities();
            ObjectSet<TEntity> _ObjectSet = context.CreateObjectSet<TEntity>();
            TEntity company = _ObjectSet.First();
                  return company;

The Service Contract is shown below.

    public interface IService1
        void Add(TEntity entity);

        TEntity Get(TEntity entity);


Creating the Client

Now let's create a client application that will consume the WCF service. 

Create a Console application using Visual Studio 2010. 


Next, add reference to the WCF service to the project so we can access the WCF service's methods.


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. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Client.DataServiceReference;
using GenericDal;

namespace Client
    class Program
        static void Main(string[] args)
            Service1Client client = new Service1Client();
             GenericDal.TEntity entity = new GenericDal.Article();


Now build and run the project.


The Run fails and we get the above error when we debug. There seems to be a error in the serialization in WCF. 

Let's figure out how to resolve this problem in my next article

Similar Articles