Поделиться через


Durable Services

Download sample

This sample demonstrates how to create a durable service by implementing a calculator as a durable service. Because the service is durable after every call, the state of the service is saved. If the service recycles, its state can be restored to the most recent persistence point and thus a persistence service is required.

Note

This sample requires that .NET Framework version 3.5 is installed to build and run. Visual Studio 2008 is required to open the project and solution files.

For more information about setting up this sample, see One-Time Set Up Procedure for the Windows Communication Foundation Samples. You must run the calculator client with this service. For more information, see the set up procedures at the end of this topic.

The following code shows the service contract for the calculator service.

[ServiceContract(Namespace = "http://Microsoft.WorkflowServices.Samples")]
    public interface ICalculator
    {
        [OperationContract()]
        int PowerOn();
        [OperationContract()]
        int Add(int value);
        [OperationContract()]
        int Subtract(int value);
        [OperationContract()]
        int Multiply(int value);
        [OperationContract()]
        int Divide(int value);
        [OperationContract()]
        void PowerOff();
    }  

In the following code, the DurableServiceAttribute attribute is used in the service implementation to specify that it is a durable service.

[Serializable]
[DurableServiceBehavior]
public class DurableCalculator : ICalculator
{
    int currentValue = default(int);

    [DurableOperationBehavior(CanCreateInstance=true)]
    public int PowerOn()
    {
        return currentValue;
    }

    [DurableOperationBehavior()]
    public int Add(int value)
    {
        return (currentValue += value);
    }

    [DurableOperationBehavior()]
    public int Subtract(int value)
    {
        return (currentValue -= value);
    }

    [DurableOperationBehavior()]
    public int Multiply(int value)
    {
        return (currentValue *= value);
    }

    [DurableOperationBehavior()]
    public int Divide(int value)
    {
        return (currentValue /= value);
    }

    [DurableOperationBehavior(CompletesInstance=true)]
    public void PowerOff()
    {
    }

}

The DurableOperationAttribute attribute specifies that the instance state is saved after the operation is complete.

There are two fields that can be specified on the DurableOperationAttribute attribute.

The CanCreateInstance property specifies that the instance is created when that operation is invoked. For CanCreateInsatance to function properly, the operation on which it is specified must be a request/response operation. In this sample, the context is propagated to the client and the durability is achieved by the client's capability to communicate with the service. If the operation is one-way, the context is not propagated to the client; therefore there must be another way to pass the context to the client if a long-running and durable conversation is to be conducted with the service instance.

The CompletesInstance property specifies that the instance is completed after the operation on which this is specified completes. The state is also deleted from the database after the operation is complete.

The following code shows the .svc file for hosting this sample in Internet Information Services (IIS).

<%@ServiceHost language=c# Debug="true" Service="Microsoft.WorkflowServices.Samples.DurableCalculator" Factory="System.ServiceModel.Activation.ServiceHostFactory" %>

The bindings for the service are configured in the Web.config file. Two endpoints with different bindings are exposed: a BasicHttpContextBinding with cookies enabled and the WSHttpContextBinding for clients that do not want to use the BasicHttpContextBinding. The WSHttpContextBinding binding helps maintain the context that is used to route requests to a particular durable service instance. The PersistenceProvider entry that specifies the persistence provider is located in the ServiceBehaviors section.

To set up and run the service

  1. To properly set up this sample using scripts, see One-Time Set Up Procedure for the Windows Communication Foundation Samples.

  2. In IIS, enable Windows Authentication on the ServiceModelSamples virtual directory.

    To enable Windows Authentication in IIS 5.1 or 6.0:

    1. Open a command prompt window and type start inetmgr to open the Internet Information Services (IIS) MMC snap-in.

    2. Right-click the virtual root ServiceModelSamples inside Default Web Site, click Properties, and then click the Directory Security tab.

    3. Under Authentication and Access Control, click Edit.

    4. In the Authentication Methods dialog box, select Integrated Windows Authentication.

    To enable Windows Authentication in IIS 7.0:

    1. Open a command prompt window and type start inetmgr to open the Internet Information Services (IIS) MMC snap-in.

    2. Select the ServiceModelSamples virtual root inside Default Web Site.

    3. Inside the ServiceModelSamples home pane, double-click Authentication inside the IIS group.

    4. Select Windows Authentication and select the Enable action.

  3. Build the project. The project builds and updates ServiceModelSamples.

  4. To allow access to the durable store:

    1. Run the CreateStores.cmd script located in the One-Time Set Up Procedure for the Windows Communication Foundation Samples topic. This sample uses the NetFx35Samples_DurableServiceStore database.

    2. Make the ASP.NET user account a member of the SQL Server user group.

  5. To ensure that the service is properly installed, point to the address https://localhost/ServiceModelSamples/service.svc. You should see the help page for the service. To look at the Web Services Descriptor Language (WSDL), type https://localhost/ServiceModelSamples/service.svc?wsdl.

  6. To run this sample, you must use the Calculator Client Sample. It is a calculator user interface created using Windows Presentation Foundation (WPF) that acts as a client for the service. You can use different endpoints that correspond to the bindings the service provides. To change the bindings, click the EndPoint menu item and select the appropriate binding, either BasicHttpContextBinding or WSHttpContextBinding.

  7. To test the durable nature of the service, close the client and reopen it while the calculator client is running. The calculator client communicates back to the same service instance and displays the instance ID at the bottom. The calculator client uses a text file called Client.ctx to store the context in a durable place the first time a call is made (in this case, in the \bin directory of your sample). When you reopen the client, it checks whether the file is present. If the file is present, it applies the stored context to the channel that is being created. If the durable service has finished and you open the client with the Client.ctx file still in your \bin directory, it attempts to apply the context to the channel. You receive an error because the durable service instance with which you want to communicate is not there. Delete the file and try again.

  8. You can also recycle the durable service by restarting IIS. Because you are using a persistence store after every operation, the state of the service is stored. Therefore, when you try to communicate with the service from the client after IIS has restarted, the infrastructure receives the durable service instance from the persistence store and you can communicate with the same instance.

    Note

    When you invoke an operation for the first time after restarting IIS, you receive a MessageSecurityException exception, which is caused by an expired security token on the channel. Invoke another operation and it will succeed.

© 2007 Microsoft Corporation. All rights reserved.