Partilhar via


Task 1: Define and Implement the Durable Service Contract

In this task, you will define and implement the service contract for your durable service.

Defining the contract

  1. Open Visual Studio 2008, click File, and select New and then Project.

  2. In the New Project dialog box, under WCF, select the WCF Service Library template.

  3. Name your project SimpleDurableService and click OK.

    Visual Studio 2008 generates the following files for your WCF service: an App.config file to store configuration settings, a source file that contains a contract definition, and a source file that contains the service class that implements the contract definition.

    The template creates a service with two operations and a data contract for a custom type. In this tutorial, you will define new operations for your durable service.

  4. Open IService1.cs (or IService1.vb if you created a Visual Basic solution) and replace the existing interface definition with the following code and remove the existing DataContractAttribute definition.

<ServiceContract()> _
Public Interface IService1

    <OperationContract()> _
    Function Add(ByVal n1 As Integer) As Integer

    <OperationContract()> _
    Function Subtract(ByVal n1 As Integer) As Integer

    <OperationContract()> _
    Function Multiply(ByVal n1 As Integer) As Integer

    <OperationContract()> _
    Sub EndPersistence()

End Interface
[ServiceContract]
public interface IService1
{
    [OperationContract]
    int Add(int n1);

    [OperationContract]
    int Subtract(int n1);

    [OperationContract]
    int Multiply(int n1);

    [OperationContract]
    void EndPersistence();
}
  1. Open Service1.cs (or Service1.vb if you created a Visual Basic solution).

  2. Implement the updated IService1 interface in your Service1 class definition.

    Visual Studio automatically creates default implementations for your interface methods, which you will modify in the next step.

  3. Modify the existing class definition to perform the appropriate logic as shown in the following code:

Public Class Service1
    Implements IService1

    Private currentValue As Integer

    Public Function Add(ByVal n1 As Integer) As Integer Implements IService1.Add
        currentValue += n1
        Return currentValue
    End Function

    Public Sub EndPersistence() Implements IService1.EndPersistence

    End Sub

    Public Function Multiply(ByVal n1 As Integer) As Integer Implements IService1.Multiply
        currentValue *= n1
        Return currentValue
    End Function

    Public Function Subtract(ByVal n1 As Integer) As Integer Implements IService1.Subtract
        currentValue -= n1
        Return currentValue
    End Function
End Class
public class Service1 : IService1
{
    int currentValue = default(int);

    #region IService1 Members

    public int Add(int n1)
    {
        return (currentValue += n1);
    }

    public int Subtract(int n1)
    {
        return (currentValue -= n1);
    }

    public int Multiply(int n1)
    {
        return (currentValue *= n1);
    }

    public void EndPersistence()
    {
    }
    #endregion
}

At this point, the WCF service you created is exactly the same as any WCF service you created in .NET Framework 3.0. The next task will show you how to take this service and persist its current state to a persistence store so that in the event your service is disconnected from the client, you can communicate with the client again at a later time from the last successful operation that was invoked.

See Also

Other Resources

Task 2: Enable Persistence for the Durable Service
Tutorial: Create a Durable Service

Copyright © 2007 by Microsoft Corporation. All rights reserved.
Last Published: 2010-03-04