JG Vimalan's Blog

It is all about coding!

Step by step: Create a simple WCF Service

In this post, I am going to explain creating a WCF service in step by step,

a. Create a WCF Service Application as shown below,

 

b.  Delete any .cs file created but keep the .svc file

The .svc file should be changed to point to the service implementation as shown below.

<%@ ServiceHost Language=”C#” Debug=”false” Service=”WcfServiceLibrary1.Service1″ %> 

The project should not contain a global.asax file.

c. Create a WCF Service Library project which will be used by the SVC file as shown in step b.

d. In the WCF Servce Library project, define the methods as shown below,

    [ServiceContract]
    public interface IService1
    {
        /// <summary>
        /// Returns the basic details of all employees
        /// </summary>
        /// <returns>list of all employees</returns>
        [OperationContract]
        List<Employee> GetAllEmployeeBasicDetails();

        /// <summary>
        /// Fetches the full name of an employee by employee id.
        /// </summary>
        /// <returns>full name of an employee</returns>
        [OperationContract]
        string GetEmployeeFullNameById(string Id);

 Add the attribute [OperationContract] for all the methods.

e. Implement the IService1 interface members in a class as shown below,

public class Service1:IService1
    {
        List<Employee> IEmployeeManager.GetAllEmployeeBasicDetails()
        {
            try
            {
                return new DataService().GetAllEmployee();
            }
            catch
            {
                //handle exception here
                return null;
            }
        }

        string IEmployeeManager.GetEmployeeFullNameById(string Id)
        {
            return “”;
        }

f. Add a class library project which will interact with the datasource. This project will be used by WCF Service Library application.

g. Creat a project to hold the entities. The entities (classes) should be created with DataContract attribure and Properties should contain DataMember attribute as shown below,

    [DataContract]
    public class Employee
    {
        [DataMember]
        public string EmployeeId { get; set; }
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public DateTime DateOfBirth { get; set; }
        [DataMember]
        public string Gender { get; set; }      
    }

h. Finally, the complete structure of your WCF Service project will be as shown below,

This is all you need to do for creating a WCF service. I will explain the binding concepts, hosting, security and advanced features in my upcoming posts.

Advertisements

October 13, 2010 - Posted by | WCF

6 Comments »

  1. Greetings, this is a genuinely absorbing web blog and I have cherished studying many of the content and posts contained on the web site, keep up the outstanding work and desire to read a good deal more stimulating articles in the future.

    Comment by kurfaimi | November 1, 2010 | Reply

  2. thanks for sharing this great article… I have saved your website to my laptop. I will look out for more excellent posts like this on your website.

    Comment by robert hyne | November 19, 2010 | Reply

  3. hi I was luck to find your subject in digg
    your post is quality
    I get much in your blog

    Comment by set365 | November 23, 2010 | Reply

  4. fantastic article I feel happy to visit your blog

    Comment by robinson | December 11, 2010 | Reply

  5. I usually don’t post in Blogs but your blog forced me to, good post…

    Comment by Bill | December 28, 2010 | Reply

  6. very good put up, i definitely love this website, carry on it

    Comment by william | January 25, 2011 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: