SessionStateModule.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. config.Mode == SessionStateMode.SQLServer)
  36. throw new NotSupportedException ("Only Off and InProc modes supported.");
  37. if (config.Mode == SessionStateMode.InProc)
  38. handlerType = typeof (SessionInProcHandler);
  39. }
  40. app.AddOnAcquireRequestStateAsync (
  41. new BeginEventHandler (OnBeginAcquireState),
  42. new EndEventHandler (OnEndAcquireState));
  43. app.ReleaseRequestState += new EventHandler (OnReleaseRequestState);
  44. app.EndRequest += new EventHandler (OnEndRequest);
  45. if (handlerType != null && handler == null) {
  46. handler = (ISessionHandler) Activator.CreateInstance (handlerType);
  47. handler.Init(app); //initialize
  48. }
  49. }
  50. void OnReleaseRequestState (object o, EventArgs args)
  51. {
  52. }
  53. void OnEndRequest (object o, EventArgs args)
  54. {
  55. }
  56. IAsyncResult OnBeginAcquireState (object o, EventArgs args, AsyncCallback cb, object data)
  57. {
  58. HttpApplication application = (HttpApplication) o;
  59. HttpContext context = application.Context;
  60. bool isNew = false;
  61. if (handler != null)
  62. isNew = handler.UpdateContext (context);
  63. // In the future, we might want to move the Async stuff down to
  64. // the interface level, if we're going to support other than
  65. // InProc, we might actually want to do things async, now we
  66. // simply fake it.
  67. HttpAsyncResult result=new HttpAsyncResult (cb,this);
  68. result.Complete (true, o, null);
  69. if (isNew && Start != null)
  70. Start (this, args);
  71. return result;
  72. }
  73. void OnEndAcquireState (IAsyncResult result)
  74. {
  75. }
  76. internal void OnEnd ()
  77. {
  78. if (End != null)
  79. End (this, EventArgs.Empty);
  80. }
  81. public event EventHandler Start;
  82. public event EventHandler End;
  83. }
  84. }