public async Task RouteNotification(IHandlerDescriptor handler, Notification notification) { using (var scope = _serviceScopeFactory.CreateScope()) { var context = scope.ServiceProvider.GetRequiredService <IRequestContext>(); context.Descriptor = handler; object @params = null; if (!(handler.Params is null)) { @params = notification.Params.ToObject(handler.Params, _serializer.JsonSerializer); } await MediatRHandlers.HandleNotification(scope.ServiceProvider.GetRequiredService <IMediator>(), handler, @params ?? EmptyRequest.Instance, CancellationToken.None).ConfigureAwait(false); } }
protected virtual async Task <ErrorResponse> RouteRequest(IHandlerDescriptor handler, Request request, CancellationToken token) { using (var scope = _serviceScopeFactory.CreateScope()) { var context = scope.ServiceProvider.GetRequiredService <IRequestContext>(); context.Descriptor = handler; if (request.Method is null) { return(new MethodNotFound(request.Id, request.Method)); } object @params; try { @params = request.Params.ToObject(handler.Params, _serializer.JsonSerializer); } catch { return(new InvalidParams(request.Id)); } var result = MediatRHandlers.HandleRequest(scope.ServiceProvider.GetRequiredService <IMediator>(), handler, @params, token); await result.ConfigureAwait(false); object responseValue = null; if (result.GetType().GetTypeInfo().IsGenericType) { var property = typeof(Task <>) .MakeGenericType(result.GetType().GetTypeInfo().GetGenericArguments()[0]).GetTypeInfo() .GetProperty(nameof(Task <object> .Result), BindingFlags.Public | BindingFlags.Instance); responseValue = property.GetValue(result); if (responseValue?.GetType() == typeof(Unit)) { responseValue = null; } } return(new Client.Response(request.Id, responseValue)); } }