WCF service – how to start?

WCF service allows you to use a huge functionality even though you have a slow local machine. Sounds good? Yes, it does. Let’s create a simple WCF service to add people. All coding is done with VS 2010 Ultimate.

What you need to do first is to create a new WCF Service Library.

1

When it is already done, please delete all default classes, which creator has added to your project automatically. Now follow these steps:

1) Prepare a simple class which will be responsible for a new object. Name it Person.cs – now add these lines of code:

namespace PeopleWcfService
{
using System.Runtime.Serialization;

[DataContract]
public class Person
{
[DataMember]
public string ItsName { get; set; }

[DataMember]
public string ItsSurname { get; set; }
}
}

DataContract contains DataMembers – it allows to exchange data between client and server, so you need to remember to put it in each class responsible for new objects.

2) Create interface with methods responsible for adding people to a group and getting the list of them. Add a new interface to your project and name it IPersonWcfService.cs. Now add this code inside:

using System.Collections.Generic;

namespace PeopleWcfService
{
    using System.ServiceModel;

    [ServiceContract]
    public interface IPersonWcfService
    {
        [OperationContract]
        void AddPerson(Person person);

        [OperationContract]
        List<Person> GetPeople();
    }
}

I always create interfaces (recommended) to define ServiceContracts,  but you can also use classes. Please remember to add [OperationContract] to each method.

3) The last thing to do is to add code to our interface’s methods. Please create a new class called PersonWcfService.cs and add this code behind:

using System.Collections.Generic;

namespace PeopleWcfService
{
    public class PersonWcfService : IPersonWcfService
    {
        private readonly List<Person> peopleList = new List<Person>();

        #region IPersonWcfService Members

        public void AddPerson(Person person)
        {
            peopleList.Add(person);
        }

        public List<Person> GetPeople()
        {
            return peopleList;
        }

        #endregion
    }
}

4) When previous steps are already finished you should build your application. When it is built please edit App.config file for your project. Right-click at App.config and select as below:

2

 

And now you need to configure your WCF service:

3

 

When you select (…) (2nd red point) navigate to bin/Debug and click twice on PersonWcfService.dll. Then you will be able to select your interface (PeopleWcfService.IPersonWcfService) -> click Open button.

Another thing that you have to do is to configure this:

4

 

Same steps as for previous configuration. Go to bin/Debug and select PeopleWcfService.dll and then PeopleWcfService.PersonWcfService. Save your configuration and close App.config

5) Start debugging. You will be able to see the new window (Test client – you can also prepare your own client, but it is the subject for another article) with your methods inside. Click at AddPerson() method and fill ItsName and ItsSurname properties. Then click Invoke button:

5

 

Go to GetPeople() method and select Invoke button. Now you can see [0] values:

6

That’s all. You have just created your first own WCF service with a Test client 🙂

 

Jedna uwaga do wpisu “WCF service – how to start?

  1. Hey MJ-

    Your last post [WCF SERVICE – HOW TO START?] was freaking awesome. I have gone ahead and added your stuff to my Feedly account. Please keep me updated if you post anywhere else.

    Keep rocking –

    Jon

Zostaw komentarz

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Wyloguj /  Zmień )

Zdjęcie na Google+

Komentujesz korzystając z konta Google+. Wyloguj /  Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj /  Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj /  Zmień )

Connecting to %s