Aspnetcore.docs: SignalR IHubContext via Middleware Suggestion

Created on 12 May 2020  ·  9Comments  ·  Source: dotnet/AspNetCore.Docs

The example which shows how to get an IHubContext instance from the middleware pipeline doesn't show the required call to next.Invoke(). Using the documentation as is results in portions of aspnetcore silently failing.

The example currently shows:
```C#
app.Use(async (context, next) =>
{
var hubContext = context.RequestServices
.GetRequiredService>();
//...
});

Changing it to the following might save some other users a few hours of debugging:
```C#
app.Use(async (context, next) =>
{
    var hubContext = context.RequestServices
                            .GetRequiredService<IHubContext<MyHub>>();
    if (next != null) {
        await next.Invoke();
    }
});

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

  • ID: 7bf6fefc-557e-114a-a37c-90939e06b083
  • Version Independent ID: 92da23e0-6dc5-170e-b489-2ad8bbd46dff
  • Content: SignalR HubContext
  • Content Source: aspnetcore/signalr/hubcontext.md
  • Product: aspnet-core
  • Technology: aspnetcore-signalr
  • GitHub Login: @bradygaster
  • Microsoft Alias: bradyg
SignalR Source - Docs.ms

All 9 comments

Good catch, @caseymarquis. Bringing in @BrennanConroy from our dev team here. Brennan - this looks like we may have cut the sample off too early. This look good to you? If so I'll either request @caseymarquis submit a pull request, or i'll do it. :)

:+1:

It should be constructor injected...

@davidfowl mmhmm. agreed and will alter the example with that in mind should I be the one to do the editing. (cloning the repo again now in preparation for this very thing)

Sounds like you're suggesting something like this?

```C#
var hostBuilder = Host.CreateDefaultBuilder();
var host = hostBuilder.ConfigureWebHostDefaults(webBuilder => {
//Configure the webservice...
}).Build();

var hubContext = host.Services.GetService(typeof(IHubContext));
//inject hubContext into other DI system
```

If so, I like that much better! Aside from not adding overhead to every request, it makes the host object the source of all web dependencies, and keeps pulling out web dependencies away from the nitty gritty configuration code.

Thanks guys! Let me know if there's a better way to do this, otherwise I'll watch for the change in the docs.

Edit: Probably not, since the term constructor injection was used. But that lead me to what I was looking for; so I'm good!

Oh wait this inline I just realized this was inline middleware carry on 😁

@caseymarquis based on @davidfowl's comment, i'd be open to you submitting a PR on the doc for us to review that reflects your ideas. If you've not submitted a PR to docs before I could help with the process if you need it. Or just tell us to do it and i'll jump on it. 👍

Hey, why not. I feel like this is a slippery slope that leads toward community contributions.

That said, having never done a github PR, is the procedure:

  1. Fork?
  2. Create a branch for changes?
  3. Make purposeful commits?
  4. Press some magic button which opens a PR form?

I'll see how far I get. I figure step four is the only mysterious part.

At the top right of the article, select the Edit pen icon:

image

Select the pen icon again. Save.

Was this page helpful?
0 / 5 - 0 ratings