WCF Series - Creating Your First WCF Service

In this article we are going to learn how to go about creating a WCF Service.

Let's open Visual Studio and create a new WCF Service Library as shown below:

WCF1.gif

Visual Studio will create an interface and a Service class which will implement that interface.

The Project is shown below :

WCF2.gif

The System.ServiceModel DLL contains all the classes required for WCF. It gets added to the project by default.

Visual Studio also creates two methods for us in the interface IService1.cs. Let me modify the code generated:

The interface code looks like below :

[ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetUserName(string value);
    }


Note the Attributes added on top of the Interface and the methods. These are essential. ServiceContract indicates that is going to be the Interface or Contract which the Service will implement. The OperationContract attribute is placed on the methods. Without this attribute, the method would not be recognized on the client side.

The Service class code looks like below :

public class Service1 : IService1
    {
        public string GetUserName(string value)
        {
            return string.Format("Welcome {0}", value);
        }
    }


Now Hit F5.

A few things happen now. The Service is hosted on the WCF Service Host. Visual Studio provides this for hosting Services.

WCF3.gif

A WCF Test Client opens up.

WCF4.gif

Double-Click on the method GetUserName to View the method as shown below:

WCF5.gif

Enter the Name and press Invoke. A Security Warning message pops up, say ok.

WCF6.gif

The Status bar of the WCF Test Client should show that the Service is getting Invoked.

WCF7.gif

The Response is generated as shown below :

WCF8.gif

The XML Response can be viewed by clicking on the XML tab. 

WCF9.gif

In the Next Post we wwill check out the Building Blocks of WCF.

Up Next
    Ebook Download
    View all
    Learn
    View all