Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I inject ServiceStack AuthSession into my repository classes?
    primarykey
    data
    text
    <p>I am struggling to find the correct way to inject the current instance of a <code>UserAuthSession</code> object (derived from ServiceStack's <code>AuthUserSession</code>) into my data-access repositories in order for them to automatically update change tracking fields on insert/update/delete operations.</p> <p>If I were newing-up repositories in my service code it would be a no-brainer, I would just do:</p> <pre><code>var repo = new MyRepository(SessionAs&lt;UserAuthSession&gt;()); </code></pre> <p>However, my repositories are auto-wired (injected) into the services, so the <code>UserAuthSession</code> has to be grabbed from somewhere in the lambda defined for the repository's registration with the IOC container, e.g.:</p> <pre><code>public class AppHost : AppHostBase { public override void Configure(Container container) { container.Register&lt;ICacheClient&gt;(new MemoryCacheClient()); container.Register&lt;IRepository&gt;(c =&gt; { return new MyRepository(**?????**); &lt;-- resolve and pass UserAuthSession } } } </code></pre> <p>Now, looking at the ServiceStack code for the <code>Service</code> class:</p> <pre><code> private object userSession; protected virtual TUserSession SessionAs&lt;TUserSession&gt;() { if (userSession == null) { userSession = TryResolve&lt;TUserSession&gt;(); //Easier to mock if (userSession == null) userSession = Cache.SessionAs&lt;TUserSession&gt;(Request, Response); } return (TUserSession)userSession; } </code></pre> <p>I can see that it looks up the cached session based on the current <code>Request</code> and <code>Response</code>, but those are not available to me in the lambda.</p> <p>What's the solution? Or am I approaching the problem from an entirely wrong angle?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload