System.Web.HttpContext.Current is null in GlobalRequestFilters and GlobalResponseFilters since version 5.2.0

Hi,

since ServiceStack version 5.2.0 we missed the System.Web.HttpContext.Current in the

base.GlobalRequestFilters.Add((httpReq, httpRes, requestDto) =>
{var testContext = System.Web.HttpContext.Current;...}

and

base.GlobalResponseFilters.Add((req, res, dto) =>
 {var testContext = System.Web.HttpContext.Current;...}

I don’t understand the question? System.Web.HttpContext.Current is an ASP.NET API, it’s existence is not related or impacted by ServiceStack.

Sorry I mean System.Web.HttpContext.Current is null…at the moment we use the current version ServiceStack 5.4.0

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.

Until now we couldn’t solve the issue described above. Is there a possibility to get the HttpContext as with

((HttpRequestBase)req.OriginalRequest).RequestContext.HttpContext

but outside the AppHost or ServiceStack Services like with System.Web.HttpContext.Current?

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.

Ok I fixed the issue with the following changes (targetFramework from 4.0 to 4.5) in our Web.config:

from

<system.web>
...
   <compilation targetFramework="4.0" debug="true" />
   ...
   <httpRuntime executionTimeout="3600" maxRequestLength="512000" />

to

<system.web>
...
   <compilation targetFramework="4.5" debug="true" />
   ...
   <httpRuntime targetFramework="4.5" executionTimeout="3600" maxRequestLength="512000" />

now the singleton System.Web.HttpContext.Current is available again…all seems to be ok.

1 Like

Sorry one question…why does no issue occured with the configuration of the target framework version 4.0 up to the Service Stack version 5.2 ?

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.