private async Task <ResponseBase> ExecuteAction <T>(Type handlerType, IServiceProvider serviceProvider, CommandBase command, ILogger <T> logger, HttpInformation information, ConnectionBase connection) { object handler = serviceProvider.GetService(handlerType); if (handler != null) { logger.LogInformation("Handling {0} with {1}", command.GetType().Name, handler.GetType().Name); try { if (handler is INeedsConnection handlerWithConnection) { if (handler is ExecuteCommandHandler || connection != null) { handlerWithConnection.Connection = connection; } else { logger.LogWarning("Cannot handle {0} without realtime connection", command.GetType().Name); return(command.CreateExceptionResponse <ResponseBase>( new MissingRealtimeConnectionException())); } } ResponseBase response = await(dynamic) handlerType.GetHandlerHandleMethod() .Invoke(handler, new object[] { information, command }); logger.LogInformation("Handled {0}", command.GetType().Name); if (response?.Error != null) { logger.LogWarning("The handler returned an error for {0}. Error: {1}, Message: {2}", command.GetType().Name, response.Error.Type, response.Error.Message); } return(response); } catch (Exception ex) { logger.LogError("Error handling {0}. Error:{1}\n{2}", command.GetType().Name, ex.GetType().Name, ex.Message); return(command.CreateExceptionResponse <ResponseBase>(ex)); } } else { logger.LogWarning("No handler was found to handle {0}", command.GetType().Name); return(command.CreateExceptionResponse <ResponseBase>(new HandlerNotFoundException())); } }
private async Task <ResponseBase> ExecuteAction <T>(Type handlerType, IServiceProvider serviceProvider, CommandBase command, ILogger <T> logger, HttpInformation information, ConnectionBase connection = null) { object handler = serviceProvider.GetService(handlerType); if (handler != null) { logger.LogInformation("Handling " + command.GetType().Name + " with " + handler.GetType().Name); try { if (handler is INeedsConnection handlerWithConnection) { if (handler is ExecuteCommandHandler || connection != null) { handlerWithConnection.Connection = connection; } else { logger.LogError("Cannot handle " + command.GetType().Name + " without realtime connection"); return(command.CreateExceptionResponse <ResponseBase>("Cannot handle this command without realtime connection")); } } ResponseBase response = await(dynamic) handlerType.GetHandlerHandleMethod() .Invoke(handler, new object[] { information, command }); logger.LogInformation("Handled " + command.GetType().Name); if (response?.Error != null) { logger.LogWarning("The handler returned an error for " + command.GetType().Name, response.Error); } return(response); } catch (Exception ex) { logger.LogError("Error handling " + command.GetType().Name); logger.LogError(ex.Message); return(command.CreateExceptionResponse <ResponseBase>(ex)); } } else { logger.LogError("No handler was found to handle " + command.GetType().Name); return(command.CreateExceptionResponse <ResponseBase>("No handler was found for command")); } }
private ResponseBase CreateAuthenticationResponseOrNull(Type handlerType, HttpInformation information, CommandBase command) { if (!options.CanExecuteCommand(command, information)) { return(command.CreateExceptionResponse <ResponseBase>("You are not allowed to execute this command")); } return(null); }
public static ResponseBase CreateExceptionResponse <T>(this CommandBase command, string exceptionMessage) where T : ResponseBase { return(command.CreateExceptionResponse <T>(new Exception(exceptionMessage))); }