Skip to content

Commit bf1aca1

Browse files
caseymarquisRick-AndersonBrennanConroy
authored
hubcontext.md: updates examples (#18284)
* Update hubcontext.md Failing to call next causes bad things to happen. Better to leave it in the example. * Update hubcontext.md This shows how to access IHost for use outside of aspnetcore. ie integration with other dependency injection frameworks * hubcontext.md: Move note back to original location. * Update hubcontext.md * Update aspnetcore/signalr/hubcontext.md Co-authored-by: Brennan <brecon@microsoft.com> Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Co-authored-by: Brennan <brecon@microsoft.com>
1 parent 9ab5adf commit bf1aca1

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

aspnetcore/signalr/hubcontext.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,40 @@ app.Use(async (context, next) =>
4444
var hubContext = context.RequestServices
4545
.GetRequiredService<IHubContext<MyHub>>();
4646
//...
47+
48+
if (next != null)
49+
{
50+
await next.Invoke();
51+
}
4752
});
4853
```
4954

5055
> [!NOTE]
5156
> When hub methods are called from outside of the `Hub` class, there's no caller associated with the invocation. Therefore, there's no access to the `ConnectionId`, `Caller`, and `Others` properties.
5257
58+
### Get an instance of IHubContext from IHost
59+
60+
Accessing an `IHubContext` from the web host is useful for
61+
integrating with areas outside of ASP.NET Core, for example, using 3rd party dependency injection frameworks:
62+
63+
```csharp
64+
public class Program
65+
{
66+
public static void Main(string[] args)
67+
{
68+
var host = CreateHostBuilder(args).Build();
69+
var hubContext = host.Services.GetService(typeof(IHubContext<MyHub>));
70+
host.Run();
71+
}
72+
73+
public static IHostBuilder CreateHostBuilder(string[] args) =>
74+
Host.CreateDefaultBuilder(args)
75+
.ConfigureWebHostDefaults(webBuilder => {
76+
webBuilder.UseStartup<Startup>();
77+
});
78+
}
79+
```
80+
5381
### Inject a strongly-typed HubContext
5482

5583
To inject a strongly-typed HubContext, ensure your Hub inherits from `Hub<T>`. Inject it using the `IHubContext<THub, T>` interface rather than `IHubContext<THub>`.

0 commit comments

Comments
 (0)