saritasa-nest / saritasa-dotnet-tools

Development Tools For Company'S .NET Projects.
BSD 2-Clause "Simplified" License
27 stars 13 forks source link

HandleCommandAsync does not throw exceptions #50

Closed dermeister0 closed 6 years ago

dermeister0 commented 6 years ago
using Microsoft.Extensions.DependencyInjection;
using Saritasa.Tools.Messages.Abstractions;
using Saritasa.Tools.Messages.Abstractions.Commands;
using Saritasa.Tools.Messages.Commands;
using Saritasa.Tools.Messages.Common;
using Saritasa.Tools.Messages.Queries;
using System;
using System.Reflection;
using System.Threading.Tasks;

namespace Test062100
{
    internal class Program
    {
        private static async Task Main(string[] args)
        {
            try
            {
                Saritasa.Tools.Messages.Internal.InternalLogger.IsEnabled = true;
                Saritasa.Tools.Messages.Internal.InternalLogger.LogToConsole = true;

                var p = new Program();
                await p.Run();
            }
            catch (Exception ex)
            {
                Console.WriteLine("EXCEPTION:");
                Console.WriteLine(ex.ToString());
            }
        }

        private async Task Run()
        {
            var services = new ServiceCollection();

            var pipelineContainer = new DefaultMessagePipelineContainer();
            pipelineContainer.AddCommandPipeline()
                .AddStandardMiddlewares(options =>
                {
                    options.InternalResolver.UseInternalObjectResolver = false;
                    options.SetAssemblies(typeof(Handlers).GetType().Assembly);
                    //options.SetAssemblies(typeof(Handlers).GetTypeInfo().Assembly);
                });

            pipelineContainer.AddQueryPipeline()
                .AddStandardMiddlewares(options =>
                {
                    options.InternalResolver.UseInternalObjectResolver = false;
                });

            services.AddSingleton<IMessagePipelineContainer>(pipelineContainer);
            services.AddScoped<IMessagePipelineService, DefaultMessagePipelineService>();
            services.AddScoped<Handlers>();

            var sp = services.BuildServiceProvider();
            var pipeline = sp.GetRequiredService<IMessagePipelineService>();

            var cmd = new MyCommand();
            //pipeline.HandleCommand(cmd);
            await pipeline.HandleCommandAsync(cmd);
        }
    }

    internal class MyCommand
    {
    }

    [CommandHandlers]
    internal class Handlers
    {
        public Task HandleMyCommandAsync(MyCommand command)
        {
            Console.WriteLine("HandleMyCommandAsync");
            return Task.CompletedTask;
        }
    }
}

Expected result:

EXCEPTION:
Saritasa.Tools.Messages.Commands.CommandHandlerNotFoundException: Cannot find command handler for MyCommand or it cannot be resolved. Make sure it has default public parameterless constructor or registered with your dependency injection container.
   at Saritasa.Tools.Messages.Commands.PipelineMiddlewares.CommandHandlerLocatorMiddleware.Handle(IMessageContext messageContext)
   at Saritasa.Tools.Messages.Common.MessagePipeline.InternalInvokeProcess(MessagePipeline messagePipeline, IMessageContext messageContext)
   at Saritasa.Tools.Messages.Abstractions.MessagePipelineServiceExtensions.HandleCommand(IMessagePipelineService pipelineService, Object command)
   at Test062100.Program.<Run>d__1.MoveNext() in C:\Users\anton\Desktop\Test062100\Program.cs:line 57
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Test062100.Program.<Main>d__0.MoveNext() in C:\Users\anton\Desktop\Test062100\Program.cs:line 23

Actual result: Empty.

dermeister0 commented 6 years ago
using Microsoft.Extensions.DependencyInjection;
using Saritasa.Tools.Messages.Abstractions;
using Saritasa.Tools.Messages.Abstractions.Commands;
using Saritasa.Tools.Messages.Commands;
using Saritasa.Tools.Messages.Common;
using Saritasa.Tools.Messages.Queries;
using System;
using System.Reflection;
using System.Threading.Tasks;

namespace Test062100
{
    internal class Program
    {
        private static async Task Main(string[] args)
        {
            try
            {
                Saritasa.Tools.Messages.Internal.InternalLogger.IsEnabled = true;
                Saritasa.Tools.Messages.Internal.InternalLogger.LogToConsole = true;

                var p = new Program();
                await p.Run();
            }
            catch (Exception ex)
            {
                Console.WriteLine("EXCEPTION:");
                Console.WriteLine(ex.ToString());
            }
        }

        private async Task Run()
        {
            var services = new ServiceCollection();

            var pipelineContainer = new DefaultMessagePipelineContainer();
            pipelineContainer.AddCommandPipeline()
                .AddStandardMiddlewares(options =>
                {
                    options.InternalResolver.UseInternalObjectResolver = false;
                    options.SetAssemblies(typeof(Handlers).GetTypeInfo().Assembly);
                });

            pipelineContainer.AddQueryPipeline()
                .AddStandardMiddlewares(options =>
                {
                    options.InternalResolver.UseInternalObjectResolver = false;
                });

            services.AddSingleton<IMessagePipelineContainer>(pipelineContainer);
            services.AddScoped<IMessagePipelineService, DefaultMessagePipelineService>();
            services.AddScoped<Handlers>();

            var sp = services.BuildServiceProvider();
            var pipeline = sp.GetRequiredService<IMessagePipelineService>();

            var cmd = new MyCommand();
            //pipeline.HandleCommand(cmd);
            await pipeline.HandleCommandAsync(cmd);
        }
    }

    internal class MyCommand
    {
    }

    [CommandHandlers]
    internal class Handlers
    {
        public Task HandleMyCommandAsync(MyCommand command)
        {
            Console.WriteLine("HandleMyCommandAsync");

            throw new Exception("123");

            return Task.CompletedTask;
        }
    }
}

Expected result:

HandleMyCommandAsync
EXCEPTION:
Saritasa.Tools.Messages.Abstractions.MessageProcessingException: Processing exception. ---> System.Exception: 123
   at Test062100.Handlers.HandleMyCommandAsync(MyCommand command) in C:\Users\anton\Desktop\Test062100\Program.cs:line 74
   at lambda_method(Closure , Object , Object , IServiceProvider , CancellationToken )
   at Saritasa.Tools.Messages.Common.BaseHandlerExecutorMiddleware.ExecuteHandler(Object handler, Object obj, IServiceProvider serviceProvider, MethodInfo handlerMethod)
   at Saritasa.Tools.Messages.Commands.PipelineMiddlewares.CommandHandlerExecutorMiddleware.Handle(IMessageContext messageContext)
   --- End of inner exception stack trace ---
   at Saritasa.Tools.Messages.Common.BaseHandlerExecutorMiddleware.InternalThrowProcessingException(IMessageContext messageContext, Boolean captureExceptionDispatchInfo)
   at Saritasa.Tools.Messages.Common.MessagePipeline.InternalInvokeProcess(MessagePipeline messagePipeline, IMessageContext messageContext)
   at Saritasa.Tools.Messages.Abstractions.MessagePipelineServiceExtensions.HandleCommand(IMessagePipelineService pipelineService, Object command)
   at Test062100.Program.<Run>d__1.MoveNext() in C:\Users\anton\Desktop\Test062100\Program.cs:line 58
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Test062100.Program.<Main>d__0.MoveNext() in C:\Users\anton\Desktop\Test062100\Program.cs:line 23

Actual result: Empty.

dermeister0 commented 6 years ago
diff --git a/src/Saritasa.Tools.Messages/Common/MessagePipeline.cs b/src/Saritasa.Tools.Messages/Common/MessagePipeline.cs
index b009a3b..81735d9 100644
--- a/src/Saritasa.Tools.Messages/Common/MessagePipeline.cs
+++ b/src/Saritasa.Tools.Messages/Common/MessagePipeline.cs
@@ -98,8 +98,7 @@ namespace Saritasa.Tools.Messages.Common
             messagePipeline.asyncMiddlewaresChain =
                 messagePipeline.CreateAsyncMiddlewaresChain(messagePipeline.middlewares).ToArray();
             messagePipeline.invokeDelegateAsync = InternalInvokeProcessAsync;
-            messagePipeline.invokeDelegateAsync(messagePipeline, messageContext, cancellationToken);
-            return Task.FromResult(1);
+            return messagePipeline.invokeDelegateAsync(messagePipeline, messageContext, cancellationToken);
         }

         #endregion
krasninja commented 6 years ago

Will be fixed in next release.