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:
Visual Studio will create an interface and a Service class which will implement that interface.
The Project is shown below :
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.
A WCF Test Client opens up.
Double-Click on the method GetUserName to View the method as shown below:
Enter the Name and press Invoke. A Security Warning message pops up, say ok.
The Status bar of the WCF Test Client should show that the Service is
getting Invoked.
The Response is generated as shown below :
The XML Response can be viewed by clicking on the XML tab.
In the Next Post we wwill check out the Building Blocks of WCF.