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.
![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 add the System.Data.Entity Reference to the project as well.
![GenWCF2.gif]()
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;
}
}
The Service Contract is shown below.
[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.
![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.
QQusing
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);
}
}
}
Now build and run the project.
![GenWCF5.gif]()
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.