ninject - Accessing CurrentUser on NancyContext outside Modules -
i've implemented restful service using nancyfx uses tokenauthentication keep track of logged in users. various activities need know user has sent request, added iusercontext
interface solution , in nancy project implement interface in class takes nancycontext
, returns username/claims/authentication status. said iusercontext
passed services through constructor injection using ninject.
in ninjectmodule on application level bind interface so:
bind<iusercontext>().to<usercontext>();
this required seems iusercontext
won't bound when nancy instantiates modules route discovery on first run , connecting server result in ninject error.
in bootstrapper i've overridden both requeststartup
, configurerequestcontainer
methods in order rebind iusercontext constant, so:
protected override void requeststartup(ikernel container, ipipelines pipelines, nancycontext context) { base.requeststartup(container, pipelines, context); tokenauthentication.enable(pipelines, new tokenauthenticationconfiguration(container.get<itokenizer>())); } protected override void configurerequestcontainer(ikernel container, nancycontext context) { container.rebind<iusercontext>() .toconstant(new usercontext(context)) .insingletonscope(); base.configurerequestcontainer(container, context); }
both methods run happily, being configurerequestcontainer
executed before requeststartup
. however, no matter try here, iusercontext
being passed services receive nancycontext
not relevant current request being executed, if rebindings in configurerequestcontainer
method ignored.
it gets strangers though: whenever modify module take on both iusercontext
, 1 of services requires iusercontext
, so:
public ordersmodule(iusercontext usercontext, ordersservice orderservice)
the usercontext
wrap nancycontext
of request, orderservice
receive different context, containing empty nancycontext
(as in, no request data, no user, etc.).
what's going on here? missing vital in flow of creation various kernels or nancy cache various objects?
edit: think ninject + childkernel culprit here: created nullcontext
bind in application kernel , bind proper usercontext in requeststartup
. whenever service being looked in childkernel cannot find it, pass on lookup parent kernel won't refer childkernel subsequent lookups (it seems). result: won't resolve iusercontext bound in requeststartup method , resort nullcontext i've defined on application's container.
Comments
Post a Comment