How to: Define a Service Contract
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) service and a client that can call the service. For an overview of all six of the tasks, see the Getting Started Tutorial topic.

When creating a basic WCF service, the first task is to define a contract. The 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 applied to it. If a method within an interface that has the ServiceContractAttribute does not have the OperationContractAttribute, that method is not exposed.

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

To create a Windows Communication Foundation contract with an interface

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

  2. Create a new console application project. Click the File menu and select New, Project. In the New Project dialog, select Visual Basic or Visual C#, and choose the Console Application template, and name it Service. Use the default Location.

  3. For a C# project Visual Studio creates a file called Program.cs. This class will contain an empty method called Main(). For a VB project, Visual Studio creates a file called Module1.vb with an empty subroutine called Main(). These methods are required for a console application project to build correctly, so you can safely leave them in the project.

  4. Change the default Service namespace to Microsoft.ServiceModel.Samples. To do this, right-click the project in the Solution Explorer and select Properties. Make sure that the Application tab on the left side of the Properties dialog is selected. For a C# project, type Microsoft.ServiceModel.Samples in the edit box labeled Default Namespace. For a VB project, type Microsoft.ServiceModel.Samples in the edit box labeled Root namespace. Click the File menu and select Save All to save your changes.

  5. If you are using C#, change the namespace in the generated Program.cs file to Microsoft.ServiceModel.Samples as shown in the following example.

    C#
    namespace Microsoft.ServiceModel.Samples 
    {
        class Program
        {
            static void Main(string[] args)
            {
            }
        }
    }

    If you are using VB, add a Namespace statement and an End Namespace statement to the generated Module1.vb as shown in the following example.

    Visual Basic
    Namespace Microsoft.ServiceModel.Samples
        Module Module1
            Sub Main()
            End Sub
        End Module
    End Namespace
  6. Add a reference to System.ServiceModel.dll to the project:

    1. In the Solution Explorer, right-click the References folder under the project folder and choose Add Reference.

    2. Select the .NET tab in the Add Reference dialog and scroll down until you see System.ServiceModel, select it, and click OK.

    ms731835.note(en-us,VS.90).gifNote:
    When using a command-line compiler (for example, Csc.exe or Vbc.exe), you must also provide the path to the assemblies. By default, on a computer running Windows Vista for example, the path is: Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation.

  7. Add a using statement (Imports in Visual Basic) for the System.ServiceModel namespace.

    Visual Basic
    Imports System.ServiceModel
    C#
    using System.ServiceModel;
  8. Define a new interface called ICalculator and apply the ServiceContractAttribute attribute to the interface with a Namespace value of "http://Microsoft.ServiceModel.Samples". Specifying the namespace explicitly is a best practice because it prevents the default namespace value from being added to the contract name.

    ms731835.note(en-us,VS.90).gifNote:
    When using attributes to annotate an interface or class, you can drop the "Attribute" part from the attribute name. So ServiceContractAttribute becomes [ServiceContract]

    Visual Basic
    <ServiceContract(Namespace:="http://Microsoft.ServiceModel.Samples")> _
    Public Interface ICalculator
    C#
    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
    public interface ICalculator
  9. Declare a method for each of the operations the ICalculator contract exposes (add, subtract, multiply, and divide) within the interface and apply the OperationContractAttribute attribute to each method that you want to expose as part of the public WCF contract.

    Visual Basic
        <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
    C#
    [OperationContract]
    double Add(double n1, double n2);
    [OperationContract]
    double Subtract(double n1, double n2);
    [OperationContract]
    double Multiply(double n1, double n2);
    [OperationContract]
    double Divide(double n1, double n2);

Example

The following code example shows a basic interface that defines a service contract.

Visual Basic
Imports System
' Step 5: Add the Imports statement for the System.ServiceModel namespace
Imports System.ServiceModel

Namespace Microsoft.ServiceModel.Samples
    ' Step 6: Define a service contract.
    <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
C#
using System;
// Step 5: Add the using statement for the System.ServiceModel namespace
using System.ServiceModel;
namespace Microsoft.ServiceModel.Samples
{
  // Step 6: Define a service contract.
  [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
  public interface ICalculator
  {
    // Step7: Create the method declaration for the contract.
    [OperationContract]
    double Add(double n1, double n2);
    [OperationContract]
    double Subtract(double n1, double n2);
    [OperationContract]
    double Multiply(double n1, double n2);
    [OperationContract]
    double Divide(double n1, double n2);
  }
}

Now the interface is created, proceed to How to: Implement a Windows Communication Foundation Service Contract to implement the interface. For troubleshooting information, see Troubleshooting the Getting Started Tutorial.

See Also


© 2007 Microsoft Corporation. All rights reserved.
Build Date: 2009-10-13
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Type change delete to divide      TechnoS ... BradSeverton   |   Edit   |   Show History

I think the line that says:

calculator service exposes (add, subtract, multiply, and delete)

should be udpate to

calculator service exposes (add, subtract, multiply, and divide)

Note: changed delete to divide

This has been fixed. Thx, Brad (CDSUA)

Talks about VS 2005, even though the page is specific to 2008      RichardOD   |   Edit   |   Show History
This page discusses visual Studio 2005, though it is supposed to be specific to VS 2008
Example has no module1      jllj3   |   Edit   |   Show History
Hello,

When I create a new console project, a module with a Sub Main is created. The example doesn't show this setup. Is this normal? Could it be because of the Express edition I'm using. If so, how should it be done?

Thanks,

John

what type of document?      Spider Bob   |   Edit   |   Show History
It is not clear what type of document to build this in. Modules and classes both request a sub main when compiled. When changing it over to a namespace it says you can't have a sub main but won't compile without a sub main. This is confusing.
Events and Delegates      HippyInASuit2   |   Edit   |   Show History
How can I define an event and it's delegate in my service contract?
Tags What's this?: Add a tag
Flag as ContentBug
Why run VS2008 as administrator?      MykelSilver   |   Edit   |   Show History
Tags What's this?: Add a tag
Flag as ContentBug
Processing
Page view tracker