21 Special Considerations—Web Applications for .NET Clients

By default, session-state values and information are stored in memory within the ASP.NET process. ASP.NET also provides session-state providers that allow you to use a session-state server that keeps session data in a separate process, or you can persist session state data to a SQL database. However, with ASP.NET 2.0, you can create custom session-state providers that allow you to customize how session-state data is stored in your ASP.NET applications.

Coherence for .NET includes a custom SessionStateStoreProvider implementation that uses a Coherence cache to store session state. This makes Coherence for .NET the best solution for any large ASP.NET application running within a web farm. Other options in this scenario are to use the StateServer, which introduces a single point of failure for the whole web farm, or to use the SqlServerStateProvider, which theoretically can be clustered, but is extremely slow and scales only to a certain point. Also, unlike both StateServer and SqlServerStateProvider, the CoherenceSessionProvider supports Session.End event through cache events—only the InProc one supports this, but it cannot be used in a web farm environment.

The only requirement of the CoherenceSessionStore is that all objects stored in the session must be serializable (.NET serializable, not POF). This same requirement applies to both out-of-proc session stores provided by Microsoft, so modifying any existing ASP.NET 2.0 application that uses StateServer or SqlServerStateProvider to use the CoherenceSessionStore is as simple as adding the following to the Web.config file:

Example 21-1 Modifying an ASP.NET Application to use CoherenceSessionStore

<sessionState mode="Custom" customProvider="CoherenceSessionProvider" timeout="20">
    <providers>
        <add name="CoherenceSessionProvider" type="Tangosol.Web.CoherenceSessionStore, Coherence" cacheName="dist-session-cache"/>
    </providers>
</sessionState>

Note that no code changes are required within the application itself.

CoherenceSessionProvider doesn't support calling Session_OnEnd event by default, so to configure the provider to send this event, the sessionEndEnabled attribute should be set to true:

Example 21-2 Adding Support for the Session _OnEnd Event

<sessionState mode="Custom" customProvider="CoherenceSessionProvider" timeout="20">
  <providers>
    <add name="CoherenceSessionProvider" type="Tangosol.Web.CoherenceSessionStore, Coherence" cacheName="dist-session-cache" sessionEndEnabled="true"/>
  </providers>
</sessionState>

If your Web application uses Coherence for .NET (either directly, by using the CoherenceSessionProvider, or both), you must remember to call CacheFactory.shutdown() when your application terminates. To make this easier, Coherence for .NET includes an HTTP module that will automatically call CacheFactory.shutdown() when your web application exits. To use the CoherenceShutdownModule simply include it in your Web.config file, as illustrated in Example 21-3:

Example 21-3 Adding Support for CoherenceShutdownModule

<httpModules>
  <add name="CoherenceShutdown" type="Tangosol.Web.CoherenceShutdownModule, Coherence"/>
</httpModules>