Пример #1
0
 public Task ExecuteAsync(Request request, CancellationToken cancellationToken)
 {
     return(Task.Run(async() =>
     {
         try
         {
             var handler = FindHandler(request.Type);
             var arguments = _serialiser.DeserialiseArguments(request.Args, handler.ArgumentsType);
             var response = await handler.HandleRequestAsync(request.Id, arguments);
             _target.Post(_channelMessageFactory.CreateResponseMessage(response));
         }
         catch (NoRequestHandlerFoundException ex)
         {
             Console.Error.WriteLine(ex.Message);
             throw;
         }
         catch (HandlerFailedException ex) {
             _target.Post(_channelMessageFactory.CreateRequestFailedChannelMessage(request.Id, ex.Error));
         }
         catch (Exception ex)
         {
             _target.Post(_channelMessageFactory.CreateRequestFailedChannelMessage(request.Id, ex));
         }
     }, cancellationToken));
 }
Пример #2
0
 public Task ExecuteAsync(Request request, CancellationToken cancellationToken)
 {
     return(Task.Run(async() =>
     {
         try
         {
             var handler = FindHandler(request.Type);
             var arguments = _serialiser.DeserialiseArguments(request.Args, handler.ArgumentsType);
             var response = await handler.HandleRequestAsync(request.Id, arguments);
             _target.Post(new RequestExecutedChannelMessage(new RequestExecutedResult(response)));
         }
         catch (NoRequestHandlerFoundException ex)
         {
             _target.Post(new RequestExecutedChannelMessage(new RequestExecutedResult(ex)));
         }
         catch (Exception ex)
         {
             _target.Post(new RequestExecutedChannelMessage((new RequestExecutedResult(new HandlerFailedException($"Request handler for request of type '{request.Type}' failed.", ex)))));
         }
     }, cancellationToken));
 }
        public Task ExecuteAsync(Response <string> response, CancellationToken cancellationToken)
        {
            if (_responseHandlers == null || _messageDispatcherBlock == null)
            {
                throw new InvalidOperationException("ResponseHandlerExecutor invoked before being initialised");
            }

            return(Task.Run(async() =>
            {
                IResponseHandler registeredResponseHandler = null;
                lock (_responseHandlers) //TODO: (RF) Get rid of this (maybe use TPL Dataflow)
                {
                    registeredResponseHandler = _responseHandlers.SingleOrDefault(r => r.RequestId == response.Id);
                }

                if (registeredResponseHandler != null)
                {
                    try
                    {
                        if (registeredResponseHandler.IsArgumentRequiredInHandler)
                        {
                            var args = _serialiser.DeserialiseArguments(response.Result, registeredResponseHandler.ResponseArgumentType);
                            await registeredResponseHandler.HandleResponseAsync(args);
                        }
                        else
                        {
                            await registeredResponseHandler.HandleResponseAsync();
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error($"Failed to run response handler for request with id: {registeredResponseHandler.RequestId}\n{{0}}", ex);
                    }
                    lock (_responseHandlers)
                    {
                        _responseHandlers.Remove(registeredResponseHandler);
                    }
                }
            }, cancellationToken));
        }