How to: Define a Windows Communication Foundation Service Contract

This is the first of six tasks required to create a basic Windows Communication Foundation (WCF) application. For an overview of all six of the tasks, see the Getting Started Tutorial topic.

When creating a WCF service, the first task is to define a service contract. The service contract specifies what operations the service supports. An operation can be thought of as a Web service method. Contracts are created by defining a C++, C#, or Visual Basic (VB) interface. Each method in the interface corresponds to a specific service operation. Each interface must have the ServiceContractAttribute applied to it and each operation must have the OperationContractAttribute attribute applied to it. If a method within an interface that has the ServiceContractAttribute attribute does not have the OperationContractAttribute attribute, that method is not exposed by the service.

The code used for this task is provided in the example following the procedure.

Define a service contract

  1. Open Visual Studio as an administrator by right-clicking the program in the Start menu and selecting More > Run as administrator.

  2. Create a WCF Service Library project.

    1. From the File menu, select New > Project.

    2. In the New Project dialog, on the left-hand side, expand Visual C# or Visual Basic, and then select the WCF category. A list of project templates is displayed in the center section of the dialog. Select WCF Service Library.

    3. Enter GettingStartedLib in the Name textbox and GettingStarted in the Solution name textbox at the bottom of the dialog.


    If you don't see the WCF project template category, you may need to install the Windows Communication Foundation component of Visual Studio. In the New Project dialog box, click the link that says Open Visual Studio Installer. Select the Individual Components tab, and then find and select Windows Communication Foundation under the Development activities category. Choose Modify to begin installing the component.

    Visual Studio creates the project, which contains 3 files: IService1.cs (or IService1.vb), Service1.cs (or Service1.vb), and App.config. The IService1 file contains a default service contract. The Service1 file contains a default implementation of the service contract. The App.config file contains configuration needed to load the default service with the Visual Studio WCF Service Host. For more information about the WCF Service Host tool, see WCF Service Host (WcfSvcHost.exe)

  3. Open the IService1.cs or IService1.vb file and delete the code within the namespace declaration, leaving the namespace declaration. Inside the namespace declaration define a new interface called ICalculator as shown in the code below.

    using System;
    using System.ServiceModel;
    namespace GettingStartedLib
            [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
            public interface ICalculator
                double Add(double n1, double n2);
                double Subtract(double n1, double n2);
                double Multiply(double n1, double n2);
                double Divide(double n1, double n2);
    Imports System.ServiceModel
    Namespace GettingStartedLib
        <ServiceContract(Namespace:="http://Microsoft.ServiceModel.Samples")> _
        Public Interface ICalculator
            <OperationContract()> _
            Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double
            <OperationContract()> _
            Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double
            <OperationContract()> _
            Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double
            <OperationContract()> _
            Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double
        End Interface
    End Namespace

    This contract defines an online calculator. Notice the ICalculator interface is marked with the ServiceContractAttribute attribute. This attribute defines a namespace that is used to disambiguate the contract name. Each calculator operation is marked with the OperationContractAttribute attribute.

Next steps

See also