Skip to content

IHttpContextAccessor.HttpContext returns null when executing within an odata batch call. #2294

Open
@UmairB

Description

@UmairB

IHttpContextAccessor.HttpContext returns null when executing within an odata batch call.

Assemblies affected

Microsoft.AspNetCore.OData 7.5.0

Reproduce steps

Simply setup a simple odata batch pipeline

var defaultBatchHandler = new DefaultODataBatchHandler();
defaultBatchHandler.MessageQuotas.MaxNestingDepth = 2;
defaultBatchHandler.MessageQuotas.MaxOperationsPerChangeset = 10;

and create an action that uses the http context accessor:

[ODataRoutePrefix("me")]
public class MeController : ODataController
{
    private readonly IHttpContextAccessor httpContextAccessor;

    public MeController(IHttpContextAccessor httpContextAccessor)
    {
        this.httpContextAccessor = httpContextAccessor;
    }

    [EnableQuery]
    [HttpGet]
    public User Get()
    { 
        var user = this.httpContextAccessor.HttpContext.User;
        // this.HttpContext is fine
        return new User { Name = user.Identity.Name };
    }
}

the following request

POST /$batch
Content-Type: application/json
{
    "requests": [
        {
            "id": "1",
            "method": "GET",
            "url": "/me"
        }
    ]
}

Produces the error

Expected result

In the example above HttpContextAccessor.HttpContext should be equal to this.HttpContext - the http context of the controller/action.

Actual result

HttpContextAccessor.HttpContext is null.

Additional detail

I realize from the simple example above I can simply use this.HttpContext but I need to access the httpcontext in a more deeper part of my code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions