Still System.Web.HttpContext.Current has nothing to do with ServiceStack, it’s an ASP.NET API that’s only available within the context of a HTTP Request in a classic (i.e. System.Web) ASP.NET App.
I’ve tested this in an ASP.NET App and both of these do return the HttpContext:
GlobalRequestFilters.Add((req, res, dto) => {
var httpCtx = System.Web.HttpContext.Current;
});
GlobalResponseFilters.Add((req, res, dto) => {
var httpCtx = System.Web.HttpContext.Current;
});
Although you should avoid using singletons wherever possible, you can also access this via the request object with:
GlobalRequestFilters.Add((req, res, dto) => {
var httpCtx = ((HttpRequestBase)req.OriginalRequest).RequestContext.HttpContext;
});
GlobalResponseFilters.Add((req, res, dto) => {
var httpCtx = ((HttpRequestBase)req.OriginalRequest).RequestContext.HttpContext;
});
Again this is only possible in a classic (i.e. System.Web) ASP.NET App.
Yes thanks it’s quite plain to me that System.Web.HttpContext.Current has nothing to do with ServiceStack but therefore I wonder that if we change only the ServiceStack dependencies version to 5.4.0 to get such an issue? I will test it again with a small AppHost…or do you have another idea why this happens?
I don’t know why you’re seeing the issue, reasons why it would be null would be because it’s not a System.Web app or that access is not being done within the context of a HTTP Request (e.g in a background Thread).
But I wouldn’t be accessing the context via the singleton when you have access to IRequest, resolve it from OriginalRequest instead.
No you can’t access the same IRequest instance via a singleton, you can create a new IRequest instance with the .ToRequest() extension method but it wont have access to any custom items you’ve added in IRequest.Items collection.
For non-HTTP Services like MQ you can use BasicRequest() which is effectively an empty IRequest instance.
I can’t tell from here, but you can only access the HttpContext.Current singleton from the HTTP Worker Request thread, if it’s null it suggests you’re on a different Thread.