ok, finally got this working with 2 changes.

1. DefaultEngineContextFactory.ResolveRequestSession now returns a new
Session Adapter instead of null.
2. my dependency resolver is now implemented like this

        public object Resolve(CreationContext context,
ISubDependencyResolver contextHandlerResolver, ComponentModel model,
DependencyModel dependency)
        {
            var factory = new MonoRailHttpHandlerFactory();
            var http_context = HttpContext.Current;
            var request = http_context.Request;
            var url_info = factory.UrlTokenizer.TokenizeUrl
(request.FilePath, request.PathInfo, request.Url, request.IsLocal,
request.ApplicationPath);

            return factory.EngineContextFactory.Create
(factory.Container, url_info, http_context, new RouteMatch());
        }

I think the problem is that
MonoRailHttpHandlerFactory.CurrentEngineContext is just pulling the
engine from the Items collection and this was created prior to session
being initialized. or it hasn't been initialized yet, and GetHandler
wasn't called so the context and related objects haven't been created.

by creating a new engine context it's now working as expected. here is
CanResolve

        public bool CanResolve(CreationContext context,
ISubDependencyResolver contextHandlerResolver, ComponentModel model,
DependencyModel dependency)
        {
            if (model.Service != typeof(IUserStartUp)) return false;
            return dependency.TargetType == typeof (IEngineContext);
        }

as you can see I'm only using this for one class at this point, so
this won't affect my controllers.

On Feb 24, 3:33 pm, Jason Meckley <[email protected]> wrote:
> I may have found the problem
>
> public class DefaultEngineContextFactory : IEngineContextFactory
> {
> public IEngineContext Create(IMonoRailContainer container, UrlInfo
> urlInfo, HttpContext context, RouteMatch routeMatch)
>                 {
>                         IDictionary session = 
> ResolveRequestSession(container, urlInfo,
> context);
>                         IUrlBuilder urlBuilder = container.UrlBuilder;
>                         ServerUtilityAdapter serverUtility = new 
> ServerUtilityAdapter
> (context.Server);
>                         string referrer = context.Request.Headers["Referer"];
>
>                         return new DefaultEngineContext(container, urlInfo, 
> context,
>                                                         serverUtility,
>                                                         new RequestAdapter
> (context.Request),
>                                                                               
>           new ResponseAdapter(context.Response, urlInfo, urlBuilder,
> serverUtility, routeMatch, referrer),
>                                                                               
>           new TraceAdapter(context.Trace), session);
>                 }
>
>                 protected virtual IDictionary ResolveRequestSession
> (IMonoRailContainer container, UrlInfo urlInfo, HttpContext context)
>                 {
>                         return null;
>                 }
>
> }
>
> I'm going to implement ResolveRequestSession to return a session
> adapter :) I'll report the results
>
> On Feb 24, 10:33 am, Jason Meckley <[email protected]> wrote:
>
> > ran a quick test. Session is not null when referenced in
> > Global.Session_Start.
> > however Session is null when referenced in UserStartUp.start();
>
> > so this means that the engine context is resolved before session
> > starts and injected into the startup service.
> > and once the session is resolved. the context in my service doesn't
> > relate back to the current http context, correct?
>
> > any ideas around this :) Is there a context or application event I can
> > tie into like OnSessionStart?
>
> > On Feb 24, 10:02 am, "Ken Egozi" <[email protected]> wrote:
>
> > > I use the FactorySupportFacility, in conjucntion with GenericFactory, and
> > > register this:
> > > var engineContextFactory = new GenericFactory<IEngineContext>(() =>
> > > MonoRailHttpHandlerFactory.CurrentEngineContext);
> > > var engineContextFactoryName = engineContextFactory.GetType().FullName;
> > > container.Register(Component.For<GenericFactory<IEngineContext>>()
> > >                 .Instance(engineContextFactory)
> > >                         .Named(engineContextFactoryName),
> > >                   Component.For<IEngineContext>()
> > >                         
> > > .Attribute("factoryId").Eq(engineContextFactoryName)
> > >                         .Attribute("factoryCreate").Eq("Create")
> > >                 .LifeStyle.Transient);
>
> > > A subdependency resolver fir IEngineContext is a very good idea.
>
> > > As for your problem - I think it has nothing to do with MR ot windsor.
> > > On app start, the Session object might simply not be available.
>
> > > -----Original Message-----
> > > From: [email protected]
>
> > > [mailto:[email protected]] On Behalf Of Jason Meckley
> > > Sent: Tuesday, February 24, 2009 4:53 PM
> > > To: Castle Project Users
> > > Subject: Register IEngineContext in Kernel
>
> > > I have a simple service which loads users specific data into session on
> > > session start.
> > > if I place this code in the global.asax  session_start member it loads 
> > > fine.
> > > abstracting this away from the http context. I created a UserStartUp 
> > > service
> > > which takes IEngineContext and IUserRepository as ctor args.
>
> > > I then created a sub dependency resolver to resolve the context engine.
> > > which is basically an adapter around
> > > MonorailHttpHandlerFactory.CurrentEngineContext.
>
> > > however when my application starts I get an null reference exception at
> > > context.Session[key] = value;
>
> > > the context is created, but session is null.
>
> > > I call the UserStartUp service in
> > > Global : HttpAppliation
> > > {
> > >          public void Session_Start(...)
> > >          {
> > >                   Container.Resolve<IUserStartUp>().start();
> > >          }
> > > }
>
> > > [Transient]
> > > UserStartUp : IUserStartUp
> > > {
> > >          //ctor args
>
> > >          public void start()
> > >          {
> > >                   var id = context.CurrentUser.Identity.Name;
> > >                   context.Session[key] = repository.get(id).name;
> > >          }
> > > }
>
> > > again, if the code is placed directly in the Session_Start member it 
> > > works.
> > > however if I move the code into a service and call the service from
> > > Session_Start, session is null.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to