i struggling find correct way inject current instance of userauthsession
object (derived servicestack's authusersession
) data-access repositories in order them automatically update change tracking fields on insert/update/delete operations.
if newing-up repositories in service code no-brainer, do:
var repo = new myrepository(sessionas<userauthsession>());
however, repositories auto-wired (injected) services, userauthsession
has grabbed somewhere in lambda defined repository's registration ioc container, e.g.:
public class apphost : apphostbase { public override void configure(container container) { container.register<icacheclient>(new memorycacheclient()); container.register<irepository>(c => { return new myrepository(**?????**); <-- resolve , pass userauthsession } } }
now, looking @ servicestack code service
class:
private object usersession; protected virtual tusersession sessionas<tusersession>() { if (usersession == null) { usersession = tryresolve<tusersession>(); //easier mock if (usersession == null) usersession = cache.sessionas<tusersession>(request, response); } return (tusersession)usersession; }
i can see looks cached session based on current request
, response
, not available me in lambda.
what's solution? or approaching problem entirely wrong angle?
found answer in another stackoverflow post stores session built request in request/thread-scoped items
dictionary of servicestack.common.hostcontext
. .
my apphost.configure()
has following code:
// add request filter storing current session in hostcontext // accessible anywhere within scope of current request. requestfilters.add((httpreq, httpres, requestdto) => { var session = httpreq.getsession(); hostcontext.instance.items.add(constants.usersessionkey, session); }); // make userauthsession resolvable hostcontext.instance.items. container.register<userauthsession>(c => { return hostcontext.instance.items[constants.usersessionkey] userauthsession; }); // wire repository. container.register<irepository>(c => { return new myrepository(c.resolve<userauthsession>()); });
Comments
Post a Comment