1

I'm developing server(console app) on latest .Net 6 platform, using AspNetCore.signalR and in OnConnectedAsync method I want to read headers present in HttpContext, but I can't get HttpContext, I've tried:

var httpContext = Context.GetHttpContext();

But this throws compilation error. Like this method does not exist in this namespace anymore(namespace: Microsoft.AspNetCore.SignalR).

enter image description here

I also tried following approach:

var features = Context.Features.Get<HttpConnectionFeature>();

But this only gives info about Local/Remote IP Addresses which is useless when using proxy.

So I want to read all context headers, like User-Agent, Remote IP address, X-Forwarded-For and etc.

Any suggestions/solutions?

1
  • Should be var h = Context.GetHttpContext().Request.Headers["myHeader"]; Commented Jun 21, 2022 at 14:27

3 Answers 3

1
    public override Task OnConnectedAsync()
    {
        var httpCtx = Context.GetHttpContext();
        var headers = httpCtx.Request.Headers;
        return base.OnConnectedAsync();
    }

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

The problem was I was using Microsoft.AspNetCore.SignalR.Core package and it didn't contain GetHttpContextExtensions and I could not reach the HttpContext. So I replaced the package with Microsoft.AspNetCore.SignalR.

1 Comment

stackoverflow.com/a/72735505/15581227 I think this may help you know more about the package.
0

Most of the signalR packages on nuget are deprecated, like this one https://www.nuget.org/packages/Microsoft.AspNetCore.SignalR.Core

So just make sure to uninstall any nuget packages of SignalR and instead update your csproj file to include the following:

<ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.