ASP.NET Compatibility

Download sample

This sample demonstrates how to enable ASP.NET Compatibility mode in Windows Communication Foundation (WCF). Services running in ASP.NET Compatibility mode participate fully in the ASP.NET application pipeline and can make use of ASP.NET features such as file/URL authorization, session state, and the HttpContext class. The HttpContext class allows access to cookies, sessions, and other ASP.NET features. This mode requires that the bindings use the HTTP transport and the service itself must be hosted in IIS.

In this sample, the client is a console application (an executable) and the service is hosted in Internet Information Services (IIS).

Note

The WCF samples may already be installed on your machine. Check this (default) directory before continuing: <InstallDrive>:\Samples\WCFWFCardspaceIf this directory doesn’t exist, click the download sample link at the top of this page. Note that this will download and install all of the WF, WCF, and CardSpace samples, you will only have to do this once. The sample is located in the following directory <InstallDrive>:\Samples\WCFWFCardSpace\WCF\Basic\Service\Hosting\WebHost\AspNetCompatibility.

Note

The setup procedure and build instructions for this sample are located at the end of this topic.

This sample is based on the Getting Started Sample, which implements a calculator service. The ICalculator contract has been modified as the ICalculatorSession contract to allow a set of operations to be performed, while keeping a running result.

[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface ICalculatorSession
{
    [OperationContract]
    void Clear();
    [OperationContract]
    void AddTo(double n);
    [OperationContract]
    void SubtractFrom(double n);
    [OperationContract]
    void MultiplyBy(double n);
    [OperationContract]
    void DivideBy(double n);
    [OperationContract]
    double Result();
}

The service maintains state, using the feature, for each client as multiple service operations are called to perform a calculation. The client can retrieve the current result by calling Result and can clear the result to zero by calling Clear.

The service uses the ASP.NET session to store the result for each client session. This allows the service to maintain the running result for each client across multiple calls to the service.

Note

ASP.NET session state and WCF sessions are very different things. See the Session for details on WCF sessions.

The service has an intimate dependency on ASP.NET session state and requires ASP.NET compatibility mode to function correctly. These requirements are expressed declaratively by applying the AspNetCompatibilityRequirements attribute.

[AspNetCompatibilityRequirements(RequirementsMode =
                       AspNetCompatibilityRequirementsMode.Required)]
public class CalculatorService : ICalculatorSession
{
    double Result
    {  // store result in AspNet Session
       get {
          if (HttpContext.Current.Session["Result"] != null)
             return (double)HttpContext.Current.Session["Result"];
          return 0.0D;
       }
       set
       {
          HttpContext.Current.Session["Result"] = value;
       }
    }
    public void Clear()
    {
        Result = 0.0D;
    }
    public void AddTo(double n)
    {
        Result += n;
    }
    public void SubtractFrom(double n)
    {
        Result -= n;
    }
    public void MultiplyBy(double n)
    {
        Result *= n;
    }
    public void DivideBy(double n)
    {
        Result /= n;
    }
    public double Result()
    {
        return Result;
    }
}

When you run the sample, the operation requests and responses are displayed in the client console window. Press ENTER in the client window to shut down the client.

    0, + 100, - 50, * 17.65, / 2 = 441.25
    Press <ENTER> to terminate client.

To set up, build, and run the sample

  1. Be sure you have performed the One-Time Set Up Procedure for the Windows Communication Foundation Samples.

  2. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.

© 2007 Microsoft Corporation. All rights reserved.