SessionStateModule.cs 2.6 KB

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