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.

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.
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>();
- _ObjectSet.AddObject(entity);
- }
- public TEntity Get(TEntity entity)
- {
- PublishingCompanyEntities context = new PublishingCompanyEntities();
- ObjectSet<TEntity> _ObjectSet = context.CreateObjectSet<TEntity>();
- TEntity company = _ObjectSet.First();
- return company;
- }
- }
- [ServiceContract]
- public interface IService1
- {
- [OperationContract]
- void Add(TEntity entity);
- [OperationContract]
- 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();
- client.Get(entity);
- }
- }
- }
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.
gkvnadh viswanadheditedPosted Feb 13, 2013, 3:27 AMEdited Feb 13, 2013, 3:28 AM
I require your email id... urgently build my service sir
gkvnadh viswanadhPosted Feb 13, 2013, 3:27 AM
Hi Mahadesh Mahalingappa i followed your Generic WCF Services Service is created Succefully. and proxy is also generated at client. but i want to upload data to service(using Add(TEntity entity)); but at client which is generated as public void Add(ConsoleApplication1.ServiceReference1.TEntity entity) { base.Channel.Add(entity); } signatures are different so unable upload data from client. plz give me your email: i can able explore it more clearly... waiting for your reply...sir