SessionStateModule.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // System.Web.SessionState.SesionStateModule
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Stefan Görling ([email protected])
  7. //
  8. // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
  9. // (C) 2003 Stefan Görling (http://www.gorling.se)
  10. using System.Web;
  11. namespace System.Web.SessionState
  12. {
  13. [MonoTODO]
  14. public sealed class SessionStateModule : IHttpModule, IRequiresSessionState
  15. {
  16. static SessionConfig config;
  17. static Type handlerType;
  18. ISessionHandler handler;
  19. public SessionStateModule ()
  20. {
  21. }
  22. public void Dispose ()
  23. {
  24. if (handler!=null)
  25. handler.Dispose();
  26. }
  27. [MonoTODO]
  28. public void Init (HttpApplication app)
  29. {
  30. if (config == null) {
  31. config = (SessionConfig) HttpContext.GetAppConfig ("system.web/sessionState");
  32. if (config == null)
  33. config = new SessionConfig (null);
  34. if (config.Mode == SessionStateMode.StateServer)
  35. throw new NotSupportedException ("StateServer mode is not supported.");
  36. if (config.Mode == SessionStateMode.SQLServer)
  37. handlerType = typeof (SessionSQLServerHandler);
  38. if (config.Mode == SessionStateMode.InProc)
  39. handlerType = typeof (SessionInProcHandler);
  40. }
  41. app.AddOnAcquireRequestStateAsync (
  42. new BeginEventHandler (OnBeginAcquireState),
  43. new EndEventHandler (OnEndAcquireState));
  44. app.ReleaseRequestState += new EventHandler (OnReleaseRequestState);
  45. app.EndRequest += new EventHandler (OnEndRequest);
  46. if (handlerType != null && handler == null) {
  47. handler = (ISessionHandler) Activator.CreateInstance (handlerType);
  48. handler.Init(app, config); //initialize
  49. }
  50. }
  51. void OnReleaseRequestState (object o, EventArgs args)
  52. {
  53. if (handler == null)
  54. return;
  55. HttpApplication application = (HttpApplication) o;
  56. HttpContext context = application.Context;
  57. handler.UpdateHandler (context);
  58. }
  59. void OnEndRequest (object o, EventArgs args)
  60. {
  61. }
  62. IAsyncResult OnBeginAcquireState (object o, EventArgs args, AsyncCallback cb, object data)
  63. {
  64. HttpApplication application = (HttpApplication) o;
  65. HttpContext context = application.Context;
  66. bool isNew = false;
  67. if (handler != null)
  68. isNew = handler.UpdateContext (context);
  69. // In the future, we might want to move the Async stuff down to
  70. // the interface level, if we're going to support other than
  71. // InProc, we might actually want to do things async, now we
  72. // simply fake it.
  73. HttpAsyncResult result=new HttpAsyncResult (cb,this);
  74. result.Complete (true, o, null);
  75. if (isNew && Start != null)
  76. Start (this, args);
  77. return result;
  78. }
  79. void OnEndAcquireState (IAsyncResult result)
  80. {
  81. }
  82. internal void OnEnd ()
  83. {
  84. if (End != null)
  85. End (this, EventArgs.Empty);
  86. }
  87. public event EventHandler Start;
  88. public event EventHandler End;
  89. }
  90. }