Session State Service


Session state is exposed in ATL Server as a server service. By default, session state support isn t enabled, so you ll have to add the code to your ATL Server ISAPI extension yourself. As you become familiar with this code, you can use the ATL Server Project Wizard in Visual Studio .NET to generate this code for you.

CSessionStateService is the service provider that implements session state in ATL Server. To add session state support in ATL Server, you need to create and initialize an instance of this class in your ATL Server ISAPI extension class.

This is a common pattern that you ll notice about ATL Server. When you want to enable features such as session state or caching in ATL Server, you do so by adding code to your ATL Server ISAPI extension handler. You can add this code manually or use the ATL Server Project Wizard in Visual Studio .NET. ATL Server was designed to use this approach over a configuration file, registry settings, or some other dynamic format because the ultimate goal for ATL Server is to provide the highest level of flexibility and performance available in a server-side Web application framework.

ATL Server allows you to decide what features you want included in your application at the source code level, and it enables you to decide exactly how and when these features are initialized . At first, this approach might seem intimidating, but with practice you should see the performance and footprint advantages of this method. Also, please remember that you can automatically generate all of the configuration code that you ll examine in this chapter using the ATL Server Project Wizard in Visual Studio .NET.

Now that we ve provided you with a bit of background about how ATL Server was designed, consider the example in Listing 8-1.

Listing 8.1: Declaration of an ATL Server ISAPI Extension Handler
start example
 1 template <class ThreadPoolClass=CThreadPool<CIsapiWorker>,  2          class CStatClass=CNoRequestStats,  3          class HttpUserErrorTextProvider=CDefaultErrorProvider,  4          class WorkerThreadTraits=DefaultThreadTraits >  5          class CsessionstateExtension :  6 public CIsapiExtension<ThreadPoolClass,  7                       CStatClass,  8                       HttpUserErrorTextProvider,  9                       WorkerThreadTraits>  10 {  11 protected:  12     typedef CIsapiExtension<ThreadPoolClass,  12                            CStatClass,  13                            HttpUserErrorTextProvider,  14                            WorkerThreadTraits> baseISAPI;  15  16    typedef CWorkerThread<WorkerThreadTraits> WorkerThreadClass;  17    typedef CSessionStateService<WorkerThreadClass,  18                                 CMemSessionServiceImpl>  19                                 sessionSvcType;  20  21    CComObjectGlobal<sessionSvcType> m_SessionStateSvc;  22  23 public:  24    BOOL GetExtensionVersion(HSE_VERSION_INFO* pVer)  25    {  26        if (!baseISAPI::GetExtensionVersion(pVer))  27        {  28            return FALSE;  29        }  30  31        if (GetCriticalIsapiError() != 0)  32        {  33            return TRUE;  34        }  35  36        if (S_OK !=  37         m_SessionStateSvc.Initialize(&m_WorkerThread,  38                                      static_cast<IServiceProvider*>  39                                      (this)))  40        {  41            ATLTRACE("Session service failed to initialize\n");  42            TerminateExtension(0);  43  44           return  SetCriticalIsapiError(IDS_ATLSRV_CRITICAL_SESSIONSTATEFAILED);  45        }  46  47        return TRUE;  48    }  49  50    BOOL TerminateExtension(DWORD dwFlags)  51    {  52        m_SessionStateSvc.Shutdown();  53        BOOL bRet = baseISAPI::TerminateExtension(dwFlags);  54  55        return bRet;  56    }  57  58    HRESULT STDMETHODCALLTYPE QueryService(REFGUID guidService,  59                                           REFIID  riid,  60                                           void**  ppvObject)  61    {  62        if (InlineIsEqualGUID(guidService,  63                              uuidof(ISessionStateService)))  64  65        return m_SessionStateSvc.QueryInterface(riid, ppvObject);  66  67        return baseISAPI::QueryService(guidService, riid, ppvObject);  68    }  69 }; 
end example
 

Let s have a look at this code line-by-line :

  • Lines 1 through 9: This code just declares your ATL Server ISAPI extension handler class, CIsapiExtension . This handler will use all of the ATL Server defaults.

  • Lines 12 through 19: The CSessionStateService template takes two arguments. The first argument determines what class should handle expired sessions. You re using the CWorkerThread class to do this by default. You can also substitute your own class for this as well. For example, you might want to use a class that logs each expired session. The second argument determines how session state is actually implemented. The class that you pass in this argument will be responsible for creating, maintaining, and expiring sessions. ATL Server provides the CMemSessionServiceImpl and CDBSessionServiceImpl classes for you. These classes store session state in memory or in a SQL Server, respectively.

  • Line 21: All ATL Server applications will share the same session service instance.

  • Lines 36 through 45: Initialize your session state when the ISAPI extension is loaded.

  • Lines 50 through 56: Shutting down your session state service will sweep any remaining sessions and clean up properly.

  • Lines 58 through 67: QueryService is the generic way to get an interface pointer to any given service in an ATL Server ISAPI extension. This particular implementation will return the ISessionStateService interface if requested .

Configuring session state in the code may seem a bit awkward at first, but it follows the ATL Server philosophy of adding as little overhead as possible. If you don t plan on using session state services, you don t have the code in your built DLL, so you don t pay any overhead tax for it.

Now that you have your ATL Server ISAPI extension configured to expose session state, let s have a look at how you can use session state services in your ATL Server application.




ATL Server. High Performance C++ on. NET
Observing the User Experience: A Practitioners Guide to User Research
ISBN: B006Z372QQ
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net