Ejemplo n.º 1
0
 /// <summary>
 /// Handles the message and is the last stage in a handler pipeline.
 /// </summary>
 /// <param name="handlerBuilder">The <see cref="IHandlerBuilder{TMessage}"/>instance.</param>
 /// <param name="handler">The handler.</param>
 public static void Handle <TMessage>(this IHandlerBuilder <TMessage> handlerBuilder, HandlerSync <TMessage> handler)
     where TMessage : class
 {
     handlerBuilder.Handle((message, _) =>
     {
         handler(message);
         return(Task.FromResult(0));
     });
 }
        public static ICreateHandlerBuilder HandleQuery <TInput, TOutput>(
            this IHandlerBuilder <QueryMessage <TInput, TOutput> > handler, Func <TInput, CancellationToken, Task <TOutput> > query)
        {
            return(handler.Handle(async(message, ct) =>
            {
                try
                {
                    var result = await query(message.Input, ct);

                    if (ct.IsCancellationRequested)
                    {
                        message.Source.TrySetCanceled();
                    }
                    else
                    {
                        message.Source.TrySetResult(result);
                    }
                }
                catch (Exception ex)
                {
                    message.Source.TrySetException(ex);
                }
            }));
        }