Click here to Skip to main content
15,895,011 members
Articles / All Topics
Technical Blog

WCF(Windows Communication Foundation)- Our First Service

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
15 Sep 2016CPOL3 min read 5.2K   2   2
Thanks you everyone for liking and sharing my last post on WCF, we now we are aware about ABC and basics about WCF. It’s time to go ahead and write our first service and consume the same. INTRODUCTION: In this post we will create a service for a book library and consume the same in our applica

Thanks you everyone for liking and sharing my last post on WCF, we now we are aware about ABC and basics about WCF. It’s time to go ahead and write our first service and consume the same.

INTRODUCTION:

In this post we will create a service for a book library and consume the same in our application.

Let’s see how we can create our first WCF service.

  1. First  we will create an empty web application named Library, which we will use later to consume our service.t1 t2
  2. Now we have empty application ready, let’s add services to this, we will use 3 tier architecture to implement this. Now we will add a new WCF project to our application, we will name it as Library.Service.Click on Application root and select Add -> New Project, select WCF from available project types.service
  3. Now we will add 2 more WCF library projects to use as ServiceContract and DataContract respectively.
  4. First we will create DataContract project, when project is delete the existing files, and add new class file name it as Book and add the properties in it. refer the class below:
    using System.Runtime.Serialization;
    namespace Library.DataContract
    {
        [DataContract]
        public class Book
        {
            [DataMember]
            public long BookId { get; set; }
            [DataMember]
            public string Name { get; set; }
            [DataMember]
            public string Author { get; set; }
            [DataMember]
            public string Publisher { get; set; }
            [DataMember]
            public string Subject { get; set; }
        }
    }
  5. The attributes DataContract and DataMember tells the service, this class will take part in our service. If we don’t get the attributes make sure we have the reference to System.Runtime.Serialization in our project.
  6. Now let’s move to ServiceContract part, delete all existing files from ServiceContract project,and add new interface and name it as ILibraryManager, we will have 2 methods AddNewBook and RetriveBooks, which will return list of books. refer code below:
    using Library.DataContract;
    using System.Collections.Generic;
    using System.ServiceModel;
    
    namespace Library.ServiceContract
    {
        [ServiceContract]
        public interface ILibraryManager
        {
            [OperationContract]
            List<Book> AddNewBook(Book book);
            [OperationContract]
            List<Book> RetriveBooks();
        }
    }
  7. Also add the reference to Library.DataContract project referencedc
  8. Once reference is added, compile the ServiceContract project to see if we are going on correct path.
  9. Now add the reference of both ServiceContract and DataContract to our Service project referenceservice
  10. Now we need to implement the interface in our service project, delete all files from the existing project, and add new WCF Service and name it as LibraryManagerService. it will add an interface as well, remove that interface.Add the code given below in cs file
    using Library.DataContract;
    using Library.ServiceContract;
    using System;
    using System.Collections.Generic;
    
    namespace Library.Service
    {
        public class LibraryManagerService : ILibraryManager
        {
            private List<Book> _books = new List<Book>()
            {
                new Book { BookId=1,Author="Santosh",Name="Test Book",Publisher="Test Publisher",Subject="Test"  },
                    new Book {BookId=1,Author="Santosh",Name="Test Book",Publisher="Test Publisher",Subject="Test" }
            };
    
            public List<Book> AddNewBook(Book book)
            {
                _books.Add(book);
                return _books;
            }
    
            public List<Book> RetriveBooks()
            {
                return _books;
            }
        }
    }
    .
  11. Now let’s build the service project again to see if we are on right path, if build is successful, also we are using dummy data here, in real case scenario we will be using data access layer.
  12. Now browse the LibraryManagerService.svc file, we will see the metadata of service.
  13. Now let’s go to Library project, and add a new controller and name it as LibraryController, we can choose Empty MVC5 controller.
  14. Now add a new service reference to our project,right click on project and go to Add-> Service Reference, and follow the below steps .serviceref advservice
  15. Change the collection type to generic list, and add the below code in controller file 
    using Library.libraryService;
    using System.Collections.Generic;
    using System.Web.Mvc;
    
    namespace Library.Controllers
    {
        public class LibraryController : Controller
        {
            // GET: Library
            [HttpGet]
            public JsonResult Index()
            {
                List<Book> book = new List<Book>();
                using (LibraryManagerClient client = new LibraryManagerClient())
                {
                    book = client.RetriveBooks();
                }
                return Json(book, JsonRequestBehavior.AllowGet);
            }
        }
    }
  16. Now start the Library application and browse the given URL in browser we will get the list.result
  17. You can try for another operation by yourself and try.

 

CONCLUSION:

The advantage of using  the service is,we can use it locally, deploy on another server , use service do deploy and use it. This is widely used in SOA(Service Oriented Architecture), when we want to provide some services to an external client, who don’t need what implementation we have internally, and can use this according to there requirement.

In coming blogs we will explore each bindings to utilize the features. You can get code for this article from my git repositoy.

You can follow my official facebook page , also subscribe my blog for more information.

You can also mail me on santosh.yadav19613@gmail.com in case you have any questions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Synechron Technologies
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
TheSQLServerGuy15-Sep-16 18:02
TheSQLServerGuy15-Sep-16 18:02 
GeneralRe: My vote of 4 Pin
santosh.yadav19861323-Sep-16 10:06
professionalsantosh.yadav19861323-Sep-16 10:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.