zhaolei
2020-11-20 921de2254ff5712a44ed8575ee8efe34252f6603
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;
 
namespace Prow.Web.SignalR
{
    /// <summary>
    /// 异常处理 SignalR 过滤器
    /// </summary>
    public class SignalRExceptionFilter<T> : IAsyncExceptionFilter
        where T : Hub
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task OnExceptionAsync(ExceptionContext context)
        {
            var handler = context.HttpContext.RequestServices.GetRequiredService<ISignalRExceptionFilterHandler<T>>();
            var hub = context.HttpContext.RequestServices.GetRequiredService<IHubContext<T>>();
            await handler.SendAsync(hub, context.Exception).ConfigureAwait(false);
        }
    }
}