HostContext AppHost TryGetCurrentRequest is null

Hi all,

I have a global requestfilter in AppHost.cs in .Net Core Service Stack API. This request filter makes a call to one of my service class methods where I need access to the service stack session object.

The following code works when I run locally.

        var request = HostContext.AppHost.TryGetCurrentRequest();
        var session = request.SessionAs<MembershipAuthSession>();

When I publish my api to azure and run a test from postman. the request is null.

        var request = HostContext.AppHost.TryGetCurrentRequest();

Any ideas why this would be the case or is there a better way to get the session object?

Thanks,
Leeny

You can only use TryGetCurrentRequest() in AppHost’s which make the Request Context available as a singleton like ASP.NET.

See this post on how to enable it in .NET Core however it’s not recommended as it incurs a performance penalty.

Thanks Demis.
I have had a look at the link “Instead you should look at populating anything you need in a custom IAuthSession and retrieve it from that.”

Actually this is all I am trying to do, trying to get my current session object. But how do I access it in a service class outside of my request dto code?

I was only trying to get the request to do this
var session = request.SessionAs();

Leeny

You need to retrieve the session from the IRequest which is injected in all Request/Response filters and from Services, Razor Views, Validators, etc with base.Request.

If your dependency needs access to the IRequest, you need to pass it in from the Service when calling the API, e.g:

MyDep.Method(base.Request)

Although if you just need the AuthUserSession pass that instead:

MyDep.Method(base.SessionAs<AuthUserSession>())

Thank you once again.