diff --git a/Bootstrap.Admin/Controllers/Api/RegisterController.cs b/Bootstrap.Admin/Controllers/Api/RegisterController.cs index d6bab797..4da681e8 100644 --- a/Bootstrap.Admin/Controllers/Api/RegisterController.cs +++ b/Bootstrap.Admin/Controllers/Api/RegisterController.cs @@ -3,7 +3,9 @@ using Bootstrap.Security; using Longbow.Web.SignalR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.SignalR; using System.Linq; +using System.Threading.Tasks; namespace Bootstrap.Admin.Controllers.Api { @@ -27,10 +29,10 @@ namespace Bootstrap.Admin.Controllers.Api /// /// [HttpPost] - public bool Post([FromServices]ISignalRHubContext hub, [FromBody]User user) + public async Task Post([FromServices]IHubContext hub, [FromBody]User user) { var ret = UserHelper.SaveUser(user); - if (ret) hub.SendAll(new MessageBody() { Category = "Users", Message = string.Format("{0}-{1}", user.UserName, user.Description) }); + if (ret) await SignalRManager.Send(hub.Clients.All, new MessageBody() { Category = "Users", Message = string.Format("{0}-{1}", user.UserName, user.Description) }); return ret; } } diff --git a/Bootstrap.Admin/SignalR/SignalRManager.cs b/Bootstrap.Admin/SignalR/SignalRManager.cs new file mode 100644 index 00000000..04147627 --- /dev/null +++ b/Bootstrap.Admin/SignalR/SignalRManager.cs @@ -0,0 +1,34 @@ +using Bootstrap.DataAccess; +using Microsoft.AspNetCore.SignalR; +using System; +using System.Data.Common; + +namespace Bootstrap.Admin +{ + /// + /// + /// + public static class SignalRManager + { + /// + /// + /// + /// + /// + /// + public static async System.Threading.Tasks.Task Send(IClientProxy client, MessageBody args) => await client.SendAsync("rev", args); + /// + /// + /// + /// + /// + /// + public static async System.Threading.Tasks.Task Send(IHubContext context, Exception ex) where T : Hub + { + var category = "App"; + if (ex.GetType().IsSubclassOf(typeof(DbException))) category = "DB"; + var message = new MessageBody() { Category = category, Message = ex.Message }; + await Send(context.Clients.All, message); + } + } +} diff --git a/Bootstrap.Admin/Startup.cs b/Bootstrap.Admin/Startup.cs index 63a07b0c..a6833732 100644 --- a/Bootstrap.Admin/Startup.cs +++ b/Bootstrap.Admin/Startup.cs @@ -50,11 +50,13 @@ namespace Bootstrap.Admin .SetApplicationName(Configuration["ApplicationName"]) .PersistKeysToFileSystem(new DirectoryInfo(Configuration["KeyPath"])); if (Configuration["DisableAutomaticKeyGeneration"] == "True") dataProtectionBuilder.DisableAutomaticKeyGeneration(); - services.AddSignalRManager(); + services.AddSignalR().AddJsonProtocalDefault(); + services.AddSignalRExceptionFilterHandler(async (client, ex) => await SignalRManager.Send(client, ex)); services.AddMvc(options => { options.Filters.Add(); options.Filters.Add(); + options.Filters.Add>(); }).AddJsonOptions(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); @@ -83,7 +85,7 @@ namespace Bootstrap.Admin app.UseAuthentication(); app.UseBootstrapAdminAuthorization(); app.UseCacheManagerCorsHandler(); - app.UseSignalR(routes => { routes.MapHub("/NotiHub"); }); + app.UseSignalR(routes => { routes.MapHub("/NotiHub"); }); app.UseMvc(routes => { routes.MapRoute( diff --git a/Bootstrap.Client/Startup.cs b/Bootstrap.Client/Startup.cs index e1fd4210..b61e0a08 100644 --- a/Bootstrap.Client/Startup.cs +++ b/Bootstrap.Client/Startup.cs @@ -50,7 +50,7 @@ namespace Bootstrap.Client .SetApplicationName(Configuration["ApplicationName"]) .PersistKeysToFileSystem(new DirectoryInfo(Configuration["KeyPath"])); if (Configuration["DisableAutomaticKeyGeneration"] == "True") dataProtectionBuilder.DisableAutomaticKeyGeneration(); - services.AddSignalRManager(); + services.AddSignalR().AddJsonProtocalDefault(); services.AddMvc(options => { options.Filters.Add(); @@ -83,14 +83,14 @@ namespace Bootstrap.Client app.UseStatusCodePagesWithReExecute("/Home/Error/{0}"); app.UseCors(builder => builder.WithOrigins(Configuration["AllowOrigins"].Split(',', StringSplitOptions.RemoveEmptyEntries)).AllowAnyHeader().AllowAnyMethod().AllowCredentials()); - //app.UseHttpsRedirection(); + app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseAuthentication(); app.UseBootstrapAdminAuthorization(); app.UseWebSocketHandler(options => options.UseAuthentication = true); app.UseCacheManagerCorsHandler(); - app.UseSignalR(routes => { routes.MapHub("/NotiHub"); }); + app.UseSignalR(routes => { routes.MapHub("/NotiHub"); }); app.UseMvc(routes => { routes.MapRoute(