Once I've created a ChatHub
which derives from Microsoft.AspNetCore.SignalR.Hub<IChatClient>
.
All components have been located in separate .net standard library.
IChatClient
looks like (it's used for type safety):
public interface IChatClient
{
Task ReceiveChatMessage(string user, string message, DateTime sentAt, bool isMarkedAsImportant);
Task ReceiveChatActivity(string user, Activity activity, DateTime sentAt);
}
Finally I used that ChatHub
in an ASP.net core project, where the hub is configured in Startup
like this:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseCors(builder =>
{
builder.WithOrigins("https://localhost:3000")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
IdentityModelEventSource.ShowPII = true;
}
else
{
app.UseGlobalExceptionHandler();
app.UseHttpsRedirection();
app.NwebSecApiSetup();
}
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHub<ChatHub>("/api/chat");
endpoints.MapHub<EventHub>("/api/events");
});
}
Additionally, I've configured something more for SignalR in the ConfigureServices
method:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddControllers().AddControllersAsServices();
services.AddHttpContextAccessor();
services.AddConnections();
services.AddSignalR(options =>
{
options.EnableDetailedErrors = true;
})
.AddNewtonsoftJsonProtocol();
...
}
I suppose you can easily use such Hubs in other projects as well.