1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace PetStore
.Web
20 using Castle
.ActiveRecord
;
22 using Castle
.Core
.Resource
;
25 using Castle
.Windsor
.Configuration
.Interpreters
;
28 public class GlobalApplication
: HttpApplication
, IContainerAccessor
30 private static WindsorContainer container
;
32 public GlobalApplication()
34 BeginRequest
+= new EventHandler(OnBeginRequest
);
35 EndRequest
+= new EventHandler(OnEndRequest
);
36 AuthenticateRequest
+= new EventHandler(OnAuthenticateRequest
);
39 public void Application_OnStart()
41 container
= new WindsorContainer( new XmlInterpreter(new ConfigResource()) );
44 public void Application_OnEnd()
49 public void OnAuthenticateRequest(object sender
, EventArgs e
)
51 HttpContext context
= HttpContext
.Current
;
53 HttpCookie cookie
= context
.Request
.Cookies
.Get("usertoken");
57 context
.User
= PetStore
.Model
.User
.Find(Convert
.ToInt32(cookie
.Value
));
61 public IWindsorContainer Container
63 get { return container; }
66 public void OnBeginRequest(object sender
, EventArgs e
)
68 HttpContext
.Current
.Items
.Add( "nh.sessionscope", new SessionScope() );
71 public void OnEndRequest(object sender
, EventArgs e
)
73 SessionScope scope
= (SessionScope
) HttpContext
.Current
.Items
["nh.sessionscope"];
77 if (scope
!= null) scope
.Dispose();
81 HttpContext
.Current
.Trace
.Warn( "Error", "Problems with the session:" + ex
.Message
, ex
);