Skip to content

[Blazor] HttpTransportType.LongPolling vs IHttpContextAccessor #20674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cubed-it opened this issue Apr 9, 2020 · 2 comments
Closed

[Blazor] HttpTransportType.LongPolling vs IHttpContextAccessor #20674

cubed-it opened this issue Apr 9, 2020 · 2 comments
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved

Comments

@cubed-it
Copy link

cubed-it commented Apr 9, 2020

Describe the bug

I have a simple component like this:

@using Microsoft.AspNetCore.Http
@inject IHttpContextAccessor ContextAccessor

@if (!_isBearerContext)
{
    @ChildContent
}

@code {
    [Parameter]
    public RenderFragment ChildContent { get; set; }

    bool _isBearerContext = false;

    protected override void OnInitialized()
    {
        _isBearerContext = ContextAccessor.HttpContext.Request.Headers.Any(x => x.Key == "Authorization");

        base.OnInitialized();
    }

}

and now I force

endpoints.MapBlazorHub(options =>
            {
               options.Transports = HttpTransportType.LongPolling;
            });

Then the result is a NullReferenceException for ContextAccessor.HttpContext.

First of all, is this behavior meant to be?

If so, then: How can I access the copied values? aspnet/SignalR#1684 #14167

Or: What would be an alternative approach to access headers in subsequent long polling requests?
How can I uniquely match a long polling request to the first request, if e.g. I would cache the headers myself?

Thanks!

@javiercn
Copy link
Member

javiercn commented Apr 9, 2020

@cubed-it thanks for contacting us.

This is more of a signalr question than a Blazor question, so I can't really go into the details. What I can tell you is that we actively discourage you from using IHttpContextAccessor in your server-side Blazor applications since it is not reliable.

Depending on what you are trying to do, we recommend passing all the data you need from the request/context during app bootstrapping process as parameters to the root component that gets rendered in _Host.cshtml

@javiercn javiercn added area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question labels Apr 9, 2020
@ghost ghost added the Status: Resolved label Apr 9, 2020
@cubed-it
Copy link
Author

cubed-it commented Apr 9, 2020

@javiercn thanks for the quick help. I think this should work. I also just found another answer from you on pretty much the same subject ;)
#18183

Maybe the documentation should be extended in this aspect? At least I can't remember seeing a dont use IHttpContextAccessor warning anywhere. :/

@cubed-it cubed-it closed this as completed Apr 9, 2020
@ghost ghost locked as resolved and limited conversation to collaborators May 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved
Projects
None yet
Development

No branches or pull requests

2 participants