diff --git a/src/Squidex.Infrastructure/Commands/DomainObjectGrain.cs b/src/Squidex.Infrastructure/Commands/DomainObjectGrain.cs index 40d3fecf6..01958c222 100644 --- a/src/Squidex.Infrastructure/Commands/DomainObjectGrain.cs +++ b/src/Squidex.Infrastructure/Commands/DomainObjectGrain.cs @@ -14,6 +14,7 @@ using Orleans.Runtime; using Squidex.Infrastructure.EventSourcing; using Squidex.Infrastructure.Orleans; using Squidex.Infrastructure.States; +using Squidex.Infrastructure.Tasks; namespace Squidex.Infrastructure.Commands { @@ -108,17 +109,17 @@ namespace Squidex.Infrastructure.Commands protected Task CreateReturnAsync(TCommand command, Func handler) where TCommand : class, IAggregateCommand { - return InvokeAsync(command, x => Task.FromResult(handler(x)), false); + return InvokeAsync(command, handler?.ToAsync(), false); } protected Task CreateAsync(TCommand command, Func handler) where TCommand : class, IAggregateCommand { - return InvokeAsync(command, x => handler(x).ContinueWith(t => null), false); + return InvokeAsync(command, handler.ToDefault(), false); } protected Task CreateAsync(TCommand command, Action handler) where TCommand : class, IAggregateCommand { - return InvokeAsync(command, x => { handler(x); return Task.FromResult(null); }, false); + return InvokeAsync(command, handler?.ToDefault()?.ToAsync(), false); } protected Task UpdateReturnAsync(TCommand command, Func> handler) where TCommand : class, IAggregateCommand @@ -128,17 +129,17 @@ namespace Squidex.Infrastructure.Commands protected Task UpdateReturnAsync(TCommand command, Func handler) where TCommand : class, IAggregateCommand { - return InvokeAsync(command, x => Task.FromResult(handler(x)), true); + return InvokeAsync(command, handler?.ToAsync(), true); } protected Task UpdateAsync(TCommand command, Func handler) where TCommand : class, IAggregateCommand { - return InvokeAsync(command, x => handler(x).ContinueWith(t => null), true); + return InvokeAsync(command, handler?.ToDefault(), true); } protected Task UpdateAsync(TCommand command, Action handler) where TCommand : class, IAggregateCommand { - return InvokeAsync(command, x => { handler(x); return Task.FromResult(null); }, true); + return InvokeAsync(command, handler?.ToDefault()?.ToAsync(), true); } private async Task InvokeAsync(TCommand command, Func> handler, bool isUpdate) where TCommand : class, IAggregateCommand diff --git a/src/Squidex.Infrastructure/Tasks/PartitionedActionBlock.cs b/src/Squidex.Infrastructure/Tasks/PartitionedActionBlock.cs index 16fd0c779..a649ce53f 100644 --- a/src/Squidex.Infrastructure/Tasks/PartitionedActionBlock.cs +++ b/src/Squidex.Infrastructure/Tasks/PartitionedActionBlock.cs @@ -24,7 +24,7 @@ namespace Squidex.Infrastructure.Tasks } public PartitionedActionBlock(Action action, Func partitioner) - : this (ToAsync(action), partitioner, new ExecutionDataflowBlockOptions()) + : this (action?.ToAsync(), partitioner, new ExecutionDataflowBlockOptions()) { } @@ -34,7 +34,7 @@ namespace Squidex.Infrastructure.Tasks } public PartitionedActionBlock(Action action, Func partitioner, ExecutionDataflowBlockOptions dataflowBlockOptions) - : this(ToAsync(action), partitioner, dataflowBlockOptions) + : this(action?.ToAsync(), partitioner, dataflowBlockOptions) { } @@ -94,17 +94,5 @@ namespace Squidex.Infrastructure.Tasks { distributor.Fault(exception); } - - private static Func ToAsync(Action action) - { - Guard.NotNull(action, nameof(action)); - - return x => - { - action(x); - - return TaskHelper.Done; - }; - } } } diff --git a/src/Squidex.Infrastructure/Tasks/TaskExtensions.cs b/src/Squidex.Infrastructure/Tasks/TaskExtensions.cs index f6bca06ce..8d5f8a548 100644 --- a/src/Squidex.Infrastructure/Tasks/TaskExtensions.cs +++ b/src/Squidex.Infrastructure/Tasks/TaskExtensions.cs @@ -16,7 +16,43 @@ namespace Squidex.Infrastructure.Tasks { } - public static Func ToAsync(this Action action) + public static Func ToDefault(this Action action) + { + Guard.NotNull(action, nameof(action)); + + return x => + { + action(x); + + return default(TOutput); + }; + } + + public static Func> ToDefault(this Func action) + { + Guard.NotNull(action, nameof(action)); + + return async x => + { + await action(x); + + return default(TOutput); + }; + } + + public static Func> ToAsync(this Func action) + { + Guard.NotNull(action, nameof(action)); + + return x => + { + var result = action(x); + + return Task.FromResult(result); + }; + } + + public static Func ToAsync(this Action action) { return x => {