SIGN UP MEMBER LOGIN:    
ARTICLE

Generic Data Access Layer for WCF : Part 4

Posted by Mahadesh Mahalingappa Articles | WCF with C# September 19, 2011
In this article we will create a WCF Business Layer and its methods.
Reader Level:
Download Files:
 

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. 

QQ
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);
        }

    }
}


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


Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor