Proxy > Gmail Facebook Yahoo!

Defining Service Contract in WCF




In this first step we will start with defining the Service contract in WCF. The WCF service contract involves the code for defining an outline of the service by creating a user defined interface. The interface includes all the methods and functionality that the underlying WCF service will expose to its corresponding client application.
The methods defined in the interfaces created for WCF service outlines its expected functionality. When an interface is created it must have theServiceContractAttribute applied to it. The ServiceContractAttributeindicates that the interface or class is defined as a service contract in application. Each method defined inside the interfaces must have theOperationContractAttribute applied to it. TheOperationCtractAtttribute indicates that the method is defined as a part of service contract in the application.

Creating WCF Service Contract with an Interface

1. Open the Microsoft Visual C# 2010 Express and select a new Console Application template from under the File menu -> New Project option.
2. In the Name box enter WCFService as application name and press ok to create an empty console application.
3. After creating the console application, it will show the Program.cs file by default containing the following Main method code:
namespace WCFService
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}
4. Next add a new interface to the application by right-clicking on the project in solution explorer and choosing New Item under the Add menu option. Specify IArea as interface name.
5. Add a reference to System.ServiceModel.dll by right-clicking the project in solution explorer and choosing Add Reference option.
6. In the Add Reference dialog box choose the System.ServiceModeland click ok to add its reference in to the application.
7. Include the System.ServiceModel namespace at the top of the IAreainterface that we created earlier:
using System.ServiceModel;
8. Define the service contract and methods for the IArea interface as shown in the following code:
namespace WCFService
{
    [ServiceContract(Namespace = "http://WCFService")]
    public interface IArea
    {
        [OperationContract]
        double AreaOfRectangle(double length, double width);
        [OperationContract]
        double AreaOfCircle(double radius);
    }
}
Now continue to next tutorial: Implementing Service Contract in WCFto learn how to implement the methods defined in the service contract.


Responses

0 Respones to "Defining Service Contract in WCF"


Send mail to your Friends.  

Expert Feed

 
Return to top of page Copyright © 2011 | My Code Logic Designed by Suneel Kumar