From 5958e8508a8d4a6640f6c0827150cf99156aa9a8 Mon Sep 17 00:00:00 2001 From: Peter Kurfer Date: Fri, 22 Jan 2021 14:05:34 +0100 Subject: [PATCH] feat: add support for single file applications --- Motor.NET.sln | 30 ++++ .../Model/InputMessage.cs | 6 +- .../Model/OutputMessage.cs | 6 +- .../SingleOutputService.cs | 6 +- .../SingleFileMessagingService/Program.cs | 37 ++++ .../SingleFileMessagingService.csproj | 15 ++ .../DefaultHostBuilderExtensions.cs | 2 +- .../AnonymousMultiOutputService.cs | 26 +++ .../AnonymousNoOutputService.cs | 25 +++ .../AnonymousSingleOutputService.cs | 25 +++ .../Motor.Extensions.Hosting.Nano.csproj | 19 ++ .../NanoHostBuilderMultiOutputExtensions.cs | 168 ++++++++++++++++++ .../NanoHostBuilderNoOutputExtensions.cs | 157 ++++++++++++++++ .../NanoHostBuilderSingleOutputExtensions.cs | 162 +++++++++++++++++ .../DefaultHostBuilderExtensions.cs | 8 +- 15 files changed, 671 insertions(+), 21 deletions(-) create mode 100644 examples/SingleFileMessagingService/Program.cs create mode 100644 examples/SingleFileMessagingService/SingleFileMessagingService.csproj create mode 100644 src/Motor.Extensions.Hosting.Nano/AnonymousMultiOutputService.cs create mode 100644 src/Motor.Extensions.Hosting.Nano/AnonymousNoOutputService.cs create mode 100644 src/Motor.Extensions.Hosting.Nano/AnonymousSingleOutputService.cs create mode 100644 src/Motor.Extensions.Hosting.Nano/Motor.Extensions.Hosting.Nano.csproj create mode 100644 src/Motor.Extensions.Hosting.Nano/NanoHostBuilderMultiOutputExtensions.cs create mode 100644 src/Motor.Extensions.Hosting.Nano/NanoHostBuilderNoOutputExtensions.cs create mode 100644 src/Motor.Extensions.Hosting.Nano/NanoHostBuilderSingleOutputExtensions.cs diff --git a/Motor.NET.sln b/Motor.NET.sln index 311d8b809..a0e0ad65a 100644 --- a/Motor.NET.sln +++ b/Motor.NET.sln @@ -96,6 +96,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsumeAndMultiOutputPublis EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTelemetryExample", "examples\OpenTelemetryExample\OpenTelemetryExample.csproj", "{E7F2E446-E2F5-4AD9-8328-42F1B5B4E245}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Motor.Extensions.Hosting.Nano", "src\Motor.Extensions.Hosting.Nano\Motor.Extensions.Hosting.Nano.csproj", "{15711F68-D012-4727-A4D4-5738171E7831}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SingleFileMessagingService", "examples\SingleFileMessagingService\SingleFileMessagingService.csproj", "{DD34D143-DC96-426C-8D6E-8389DEC37390}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -586,6 +590,30 @@ Global {E7F2E446-E2F5-4AD9-8328-42F1B5B4E245}.Release|x64.Build.0 = Release|Any CPU {E7F2E446-E2F5-4AD9-8328-42F1B5B4E245}.Release|x86.ActiveCfg = Release|Any CPU {E7F2E446-E2F5-4AD9-8328-42F1B5B4E245}.Release|x86.Build.0 = Release|Any CPU + {15711F68-D012-4727-A4D4-5738171E7831}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {15711F68-D012-4727-A4D4-5738171E7831}.Debug|Any CPU.Build.0 = Debug|Any CPU + {15711F68-D012-4727-A4D4-5738171E7831}.Debug|x64.ActiveCfg = Debug|Any CPU + {15711F68-D012-4727-A4D4-5738171E7831}.Debug|x64.Build.0 = Debug|Any CPU + {15711F68-D012-4727-A4D4-5738171E7831}.Debug|x86.ActiveCfg = Debug|Any CPU + {15711F68-D012-4727-A4D4-5738171E7831}.Debug|x86.Build.0 = Debug|Any CPU + {15711F68-D012-4727-A4D4-5738171E7831}.Release|Any CPU.ActiveCfg = Release|Any CPU + {15711F68-D012-4727-A4D4-5738171E7831}.Release|Any CPU.Build.0 = Release|Any CPU + {15711F68-D012-4727-A4D4-5738171E7831}.Release|x64.ActiveCfg = Release|Any CPU + {15711F68-D012-4727-A4D4-5738171E7831}.Release|x64.Build.0 = Release|Any CPU + {15711F68-D012-4727-A4D4-5738171E7831}.Release|x86.ActiveCfg = Release|Any CPU + {15711F68-D012-4727-A4D4-5738171E7831}.Release|x86.Build.0 = Release|Any CPU + {DD34D143-DC96-426C-8D6E-8389DEC37390}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DD34D143-DC96-426C-8D6E-8389DEC37390}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DD34D143-DC96-426C-8D6E-8389DEC37390}.Debug|x64.ActiveCfg = Debug|Any CPU + {DD34D143-DC96-426C-8D6E-8389DEC37390}.Debug|x64.Build.0 = Debug|Any CPU + {DD34D143-DC96-426C-8D6E-8389DEC37390}.Debug|x86.ActiveCfg = Debug|Any CPU + {DD34D143-DC96-426C-8D6E-8389DEC37390}.Debug|x86.Build.0 = Debug|Any CPU + {DD34D143-DC96-426C-8D6E-8389DEC37390}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DD34D143-DC96-426C-8D6E-8389DEC37390}.Release|Any CPU.Build.0 = Release|Any CPU + {DD34D143-DC96-426C-8D6E-8389DEC37390}.Release|x64.ActiveCfg = Release|Any CPU + {DD34D143-DC96-426C-8D6E-8389DEC37390}.Release|x64.Build.0 = Release|Any CPU + {DD34D143-DC96-426C-8D6E-8389DEC37390}.Release|x86.ActiveCfg = Release|Any CPU + {DD34D143-DC96-426C-8D6E-8389DEC37390}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -631,6 +659,8 @@ Global {FA64170E-BF10-4670-84C6-1EF36E143E08} = {3DC7D216-6908-4759-B86F-759FDAE393D9} {F8386DEA-8905-4157-B949-CC151F3FB8D2} = {3DC7D216-6908-4759-B86F-759FDAE393D9} {E7F2E446-E2F5-4AD9-8328-42F1B5B4E245} = {3DC7D216-6908-4759-B86F-759FDAE393D9} + {15711F68-D012-4727-A4D4-5738171E7831} = {749B1421-3177-4C7A-A66B-541BD4E925B0} + {DD34D143-DC96-426C-8D6E-8389DEC37390} = {3DC7D216-6908-4759-B86F-759FDAE393D9} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {5E91C34C-3AEC-4084-BA02-753C9236AA34} diff --git a/examples/ConsumeAndPublishWithRabbitMQ/Model/InputMessage.cs b/examples/ConsumeAndPublishWithRabbitMQ/Model/InputMessage.cs index 82f74c9f1..f2003f9d6 100644 --- a/examples/ConsumeAndPublishWithRabbitMQ/Model/InputMessage.cs +++ b/examples/ConsumeAndPublishWithRabbitMQ/Model/InputMessage.cs @@ -1,8 +1,4 @@ namespace ConsumeAndPublishWithRabbitMQ.Model { - public record InputMessage - { - public string FancyText { get; set; } = "FooBar"; - public int FancyNumber { get; set; } = 42; - } + public record InputMessage(string FancyText = "FooBar", int FancyNumber = 42); } diff --git a/examples/ConsumeAndPublishWithRabbitMQ/Model/OutputMessage.cs b/examples/ConsumeAndPublishWithRabbitMQ/Model/OutputMessage.cs index 111d93377..d1d8be04a 100644 --- a/examples/ConsumeAndPublishWithRabbitMQ/Model/OutputMessage.cs +++ b/examples/ConsumeAndPublishWithRabbitMQ/Model/OutputMessage.cs @@ -1,8 +1,4 @@ namespace ConsumeAndPublishWithRabbitMQ.Model { - public record OutputMessage - { - public string NotSoFancyText { get; set; } - public int NotSoFancyNumber { get; set; } - } + public record OutputMessage(string NotSoFancyText, int NotSoFancyNumber); } diff --git a/examples/ConsumeAndPublishWithRabbitMQ/SingleOutputService.cs b/examples/ConsumeAndPublishWithRabbitMQ/SingleOutputService.cs index 5aa1c0498..6403595f2 100644 --- a/examples/ConsumeAndPublishWithRabbitMQ/SingleOutputService.cs +++ b/examples/ConsumeAndPublishWithRabbitMQ/SingleOutputService.cs @@ -33,11 +33,7 @@ private static OutputMessage MagicFunc(InputMessage input) throw new ArgumentNullException("FancyText is empty"); } - var output = new OutputMessage - { - NotSoFancyText = input.FancyText.Reverse().ToString(), - NotSoFancyNumber = input.FancyNumber * -1, - }; + var output = new OutputMessage(input.FancyText.Reverse().ToString(), input.FancyNumber * -1); return output; } } diff --git a/examples/SingleFileMessagingService/Program.cs b/examples/SingleFileMessagingService/Program.cs new file mode 100644 index 000000000..daf28c4d4 --- /dev/null +++ b/examples/SingleFileMessagingService/Program.cs @@ -0,0 +1,37 @@ +using System; +using System.Linq; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Motor.Extensions.Conversion.SystemJson; +using Motor.Extensions.Hosting.Consumer; +using Motor.Extensions.Hosting.Nano; +using Motor.Extensions.Hosting.Publisher; +using Motor.Extensions.Hosting.RabbitMQ; +using Motor.Extensions.Utilities; + +await MotorHost.CreateDefaultBuilder() + .ConfigureAnonymousSingleOutputService((cloudEvent, logger) => + { + logger.LogInformation("Handling message - synchronously"); + var data = cloudEvent.TypedData; + return data switch + { + { FancyText: { Length: > 0 } } => cloudEvent.CreateNew(new OutputMessage(data.FancyText.Reverse().ToString(), + data.FancyNumber * -1)), + _ => throw new ArgumentNullException("FancyText is empty") + }; + }) + .ConfigureConsumer((_, builder) => + { + builder.AddRabbitMQ(); + builder.AddSystemJson(); + }).ConfigurePublisher((_, builder) => + { + builder.AddRabbitMQ(); + builder.AddSystemJson(); + }) + .RunConsoleAsync(); + +public record OutputMessage(string NotSoFancyText, int NotSoFancyNumber); + +public record InputMessage(string FancyText = "FooBar", int FancyNumber = 42); diff --git a/examples/SingleFileMessagingService/SingleFileMessagingService.csproj b/examples/SingleFileMessagingService/SingleFileMessagingService.csproj new file mode 100644 index 000000000..c48595c5b --- /dev/null +++ b/examples/SingleFileMessagingService/SingleFileMessagingService.csproj @@ -0,0 +1,15 @@ + + + + Exe + net5.0 + + + + + + + + + + diff --git a/src/Motor.Extensions.Diagnostics.Logging/DefaultHostBuilderExtensions.cs b/src/Motor.Extensions.Diagnostics.Logging/DefaultHostBuilderExtensions.cs index ed58a4b09..41ace8c30 100644 --- a/src/Motor.Extensions.Diagnostics.Logging/DefaultHostBuilderExtensions.cs +++ b/src/Motor.Extensions.Diagnostics.Logging/DefaultHostBuilderExtensions.cs @@ -21,7 +21,7 @@ public static IMotorHostBuilder ConfigureSerilog(this IMotorHostBuilder hostBuil .WriteTo.Console(new JsonFormatter(renderMessage: true)); configuration?.Invoke(hostingContext, loggerConfiguration); }) - .ConfigureServices((hostContext, services) => + .ConfigureServices((_, services) => { services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true)); }); diff --git a/src/Motor.Extensions.Hosting.Nano/AnonymousMultiOutputService.cs b/src/Motor.Extensions.Hosting.Nano/AnonymousMultiOutputService.cs new file mode 100644 index 000000000..82eb40c22 --- /dev/null +++ b/src/Motor.Extensions.Hosting.Nano/AnonymousMultiOutputService.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Motor.Extensions.Hosting.Abstractions; + +namespace Motor.Extensions.Hosting.Nano +{ + public class AnonymousMultiOutputService : IMultiOutputService where TInput : class where TOutput : class + { + private readonly Func, CancellationToken, Task>>> _handler; + + public AnonymousMultiOutputService(Func, CancellationToken, Task>>> handler) + { + _handler = handler; + } + + public AnonymousMultiOutputService(Func, IEnumerable>> handler) + { + _handler = (cloudEvent, _) => Task.FromResult(handler(cloudEvent)); + } + + public Task>> ConvertMessageAsync(MotorCloudEvent dataCloudEvent, + CancellationToken token = default) => _handler(dataCloudEvent, token); + } +} diff --git a/src/Motor.Extensions.Hosting.Nano/AnonymousNoOutputService.cs b/src/Motor.Extensions.Hosting.Nano/AnonymousNoOutputService.cs new file mode 100644 index 000000000..5d22b41a0 --- /dev/null +++ b/src/Motor.Extensions.Hosting.Nano/AnonymousNoOutputService.cs @@ -0,0 +1,25 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Motor.Extensions.Hosting.Abstractions; + +namespace Motor.Extensions.Hosting.Nano +{ + public class AnonymousNoOutputService : INoOutputService where TInput : class + { + private readonly Func, CancellationToken, Task> _handler; + + public AnonymousNoOutputService(Func, CancellationToken, Task> handler) + { + _handler = handler; + } + + public AnonymousNoOutputService(Func, ProcessedMessageStatus> handler) + { + _handler = (cloudEvent, _) => Task.FromResult(handler(cloudEvent)); + } + + public Task HandleMessageAsync(MotorCloudEvent dataCloudEvent, + CancellationToken token = default) => _handler(dataCloudEvent, token); + } +} diff --git a/src/Motor.Extensions.Hosting.Nano/AnonymousSingleOutputService.cs b/src/Motor.Extensions.Hosting.Nano/AnonymousSingleOutputService.cs new file mode 100644 index 000000000..93a8705ca --- /dev/null +++ b/src/Motor.Extensions.Hosting.Nano/AnonymousSingleOutputService.cs @@ -0,0 +1,25 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Motor.Extensions.Hosting.Abstractions; + +namespace Motor.Extensions.Hosting.Nano +{ + public class AnonymousSingleOutputService : ISingleOutputService where TInput : class where TOutput : class + { + private readonly Func, CancellationToken, Task>> _handler; + + public AnonymousSingleOutputService(Func, CancellationToken, Task>> handler) + { + _handler = handler; + } + + public AnonymousSingleOutputService(Func, MotorCloudEvent> handler) + { + _handler = (cloudEvent, _) => Task.FromResult(handler(cloudEvent)); + } + + public Task> ConvertMessageAsync(MotorCloudEvent dataCloudEvent, + CancellationToken token = default) => _handler(dataCloudEvent, token); + } +} diff --git a/src/Motor.Extensions.Hosting.Nano/Motor.Extensions.Hosting.Nano.csproj b/src/Motor.Extensions.Hosting.Nano/Motor.Extensions.Hosting.Nano.csproj new file mode 100644 index 000000000..23c05ba28 --- /dev/null +++ b/src/Motor.Extensions.Hosting.Nano/Motor.Extensions.Hosting.Nano.csproj @@ -0,0 +1,19 @@ + + + + net5.0 + + + + + + + + + + + + + + + diff --git a/src/Motor.Extensions.Hosting.Nano/NanoHostBuilderMultiOutputExtensions.cs b/src/Motor.Extensions.Hosting.Nano/NanoHostBuilderMultiOutputExtensions.cs new file mode 100644 index 000000000..09296ddef --- /dev/null +++ b/src/Motor.Extensions.Hosting.Nano/NanoHostBuilderMultiOutputExtensions.cs @@ -0,0 +1,168 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Motor.Extensions.Hosting.Abstractions; +using Motor.Extensions.Utilities; +using Motor.Extensions.Utilities.Abstractions; + +namespace Motor.Extensions.Hosting.Nano +{ + public static class NanoHostBuilderExtensions + { + public static IMotorHostBuilder ConfigureAnonymousMultiOutputService( + this IMotorHostBuilder hostBuilder, + Func, IEnumerable>> handler) + where TInput : class + where TOutput : class + { + return hostBuilder + .ConfigureMultiOutputService() + .ConfigureServices((_, services) => + { + services.AddTransient>(_ => + new AnonymousMultiOutputService(handler)); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousMultiOutputService( + this IMotorHostBuilder hostBuilder, + Func, TDep1, IEnumerable>> + handler) + where TInput : class + where TOutput : class + where TDep1 : class + { + return hostBuilder + .ConfigureMultiOutputService() + .ConfigureServices((_, services) => + { + services.AddTransient>(provider => + new AnonymousMultiOutputService((cloudEvent) => + handler(cloudEvent, provider.GetRequiredService())) + ); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousMultiOutputService( + this IMotorHostBuilder hostBuilder, + Func, TDep1, TDep2, IEnumerable>> + handler) + where TInput : class + where TOutput : class + where TDep1 : class + where TDep2 : class + { + return hostBuilder + .ConfigureMultiOutputService() + .ConfigureServices((_, services) => + { + services.AddTransient>(provider => + new AnonymousMultiOutputService((cloudEvent) => + handler(cloudEvent, provider.GetRequiredService(), provider.GetRequiredService())) + ); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousMultiOutputService( + this IMotorHostBuilder hostBuilder, + Func, TDep1, TDep2, TDep3, + IEnumerable>> + handler) + where TInput : class + where TOutput : class + where TDep1 : class + where TDep2 : class + where TDep3 : class + { + return hostBuilder + .ConfigureMultiOutputService() + .ConfigureServices((_, services) => + { + services.AddTransient>(provider => + new AnonymousMultiOutputService((cloudEvent) => + handler(cloudEvent, provider.GetRequiredService(), provider.GetRequiredService(), + provider.GetRequiredService())) + ); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousMultiOutputService( + this IMotorHostBuilder hostBuilder, + Func, CancellationToken, Task>>> handler) + where TInput : class + where TOutput : class + { + return hostBuilder + .ConfigureMultiOutputService() + .ConfigureServices((_, services) => + { + services.AddTransient>(_ => + new AnonymousMultiOutputService(handler)); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousMultiOutputService( + this IMotorHostBuilder hostBuilder, + Func, TDep1, CancellationToken, Task>>> + handler) + where TInput : class + where TOutput : class + where TDep1 : class + { + return hostBuilder + .ConfigureMultiOutputService() + .ConfigureServices((_, services) => + { + services.AddTransient>(provider => + new AnonymousMultiOutputService((cloudEvent, token) => + handler(cloudEvent, provider.GetRequiredService(), token)) + ); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousMultiOutputService( + this IMotorHostBuilder hostBuilder, + Func, TDep1, TDep2, CancellationToken, Task>>> + handler) + where TInput : class + where TOutput : class + where TDep1 : class + where TDep2 : class + { + return hostBuilder + .ConfigureMultiOutputService() + .ConfigureServices((_, services) => + { + services.AddTransient>(provider => + new AnonymousMultiOutputService((cloudEvent, token) => + handler(cloudEvent, provider.GetRequiredService(), provider.GetRequiredService(), token)) + ); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousMultiOutputService( + this IMotorHostBuilder hostBuilder, + Func, TDep1, TDep2, TDep3, CancellationToken, + Task>>> + handler) + where TInput : class + where TOutput : class + where TDep1 : class + where TDep2 : class + where TDep3 : class + { + return hostBuilder + .ConfigureMultiOutputService() + .ConfigureServices((_, services) => + { + services.AddTransient>(provider => + new AnonymousMultiOutputService((cloudEvent, token) => + handler(cloudEvent, provider.GetRequiredService(), provider.GetRequiredService(), + provider.GetRequiredService(), token)) + ); + }); + } + } +} diff --git a/src/Motor.Extensions.Hosting.Nano/NanoHostBuilderNoOutputExtensions.cs b/src/Motor.Extensions.Hosting.Nano/NanoHostBuilderNoOutputExtensions.cs new file mode 100644 index 000000000..96110d1bc --- /dev/null +++ b/src/Motor.Extensions.Hosting.Nano/NanoHostBuilderNoOutputExtensions.cs @@ -0,0 +1,157 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Motor.Extensions.Hosting.Abstractions; +using Motor.Extensions.Utilities; +using Motor.Extensions.Utilities.Abstractions; + +namespace Motor.Extensions.Hosting.Nano +{ + public static class NanoHostBuilderNoOutputExtensions + { + public static IMotorHostBuilder ConfigureAnonymousNoOutputService( + this IMotorHostBuilder hostBuilder, + Func, ProcessedMessageStatus> handler, + string healthCheckConfigSection = "HealthCheck") + where TInput : class + { + return hostBuilder + .ConfigureNoOutputService(healthCheckConfigSection) + .ConfigureServices((_, services) => + { + services.AddTransient>(_ => + new AnonymousNoOutputService(handler)); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousNoOutputService( + this IMotorHostBuilder hostBuilder, + Func, TDep1, ProcessedMessageStatus> handler, + string healthCheckConfigSection = "HealthCheck") + where TInput : class + where TDep1 : class + { + return hostBuilder + .ConfigureNoOutputService(healthCheckConfigSection) + .ConfigureServices((_, services) => + { + services.AddTransient>(provider => + new AnonymousNoOutputService((cloudEvent) => + handler(cloudEvent, provider.GetRequiredService()))); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousNoOutputService( + this IMotorHostBuilder hostBuilder, + Func, TDep1, TDep2, ProcessedMessageStatus> handler, + string healthCheckConfigSection = "HealthCheck") + where TInput : class + where TDep1 : class + where TDep2 : class + { + return hostBuilder + .ConfigureNoOutputService(healthCheckConfigSection) + .ConfigureServices((_, services) => + { + services.AddTransient>(provider => + new AnonymousNoOutputService((cloudEvent) => handler(cloudEvent, + provider.GetRequiredService(), provider.GetRequiredService()))); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousNoOutputService( + this IMotorHostBuilder hostBuilder, + Func, TDep1, TDep2, TDep3, ProcessedMessageStatus> handler, + string healthCheckConfigSection = "HealthCheck") + where TInput : class + where TDep1 : class + where TDep2 : class + where TDep3 : class + { + return hostBuilder + .ConfigureNoOutputService(healthCheckConfigSection) + .ConfigureServices((_, services) => + { + services.AddTransient>(provider => + new AnonymousNoOutputService((cloudEvent) => handler(cloudEvent, + provider.GetRequiredService(), + provider.GetRequiredService(), + provider.GetRequiredService())) + ); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousNoOutputService( + this IMotorHostBuilder hostBuilder, + Func, CancellationToken, Task> handler, + string healthCheckConfigSection = "HealthCheck") + where TInput : class + { + return hostBuilder + .ConfigureNoOutputService(healthCheckConfigSection) + .ConfigureServices((_, services) => + { + services.AddTransient>(_ => + new AnonymousNoOutputService(handler)); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousNoOutputService( + this IMotorHostBuilder hostBuilder, + Func, TDep1, CancellationToken, Task> handler, + string healthCheckConfigSection = "HealthCheck") + where TInput : class + where TDep1 : class + { + return hostBuilder + .ConfigureNoOutputService(healthCheckConfigSection) + .ConfigureServices((_, services) => + { + services.AddTransient>(provider => + new AnonymousNoOutputService((cloudEvent, token) => + handler(cloudEvent, provider.GetRequiredService(), token))); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousNoOutputService( + this IMotorHostBuilder hostBuilder, + Func, TDep1, TDep2, CancellationToken, Task> handler, + string healthCheckConfigSection = "HealthCheck") + where TInput : class + where TDep1 : class + where TDep2 : class + { + return hostBuilder + .ConfigureNoOutputService(healthCheckConfigSection) + .ConfigureServices((_, services) => + { + services.AddTransient>(provider => + new AnonymousNoOutputService((cloudEvent, token) => handler(cloudEvent, + provider.GetRequiredService(), provider.GetRequiredService(), token))); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousNoOutputService( + this IMotorHostBuilder hostBuilder, + Func, TDep1, TDep2, TDep3, CancellationToken, Task> handler, + string healthCheckConfigSection = "HealthCheck") + where TInput : class + where TDep1 : class + where TDep2 : class + where TDep3 : class + { + return hostBuilder + .ConfigureNoOutputService(healthCheckConfigSection) + .ConfigureServices((_, services) => + { + services.AddTransient>(provider => + new AnonymousNoOutputService((cloudEvent, token) => handler(cloudEvent, + provider.GetRequiredService(), + provider.GetRequiredService(), + provider.GetRequiredService(), token)) + ); + }); + } + } +} diff --git a/src/Motor.Extensions.Hosting.Nano/NanoHostBuilderSingleOutputExtensions.cs b/src/Motor.Extensions.Hosting.Nano/NanoHostBuilderSingleOutputExtensions.cs new file mode 100644 index 000000000..04adba562 --- /dev/null +++ b/src/Motor.Extensions.Hosting.Nano/NanoHostBuilderSingleOutputExtensions.cs @@ -0,0 +1,162 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Motor.Extensions.Hosting.Abstractions; +using Motor.Extensions.Utilities; +using Motor.Extensions.Utilities.Abstractions; + +namespace Motor.Extensions.Hosting.Nano +{ + public static class NanoHostBuilderSingleOutputExtensions + { + public static IMotorHostBuilder ConfigureAnonymousSingleOutputService( + this IMotorHostBuilder hostBuilder, + Func, MotorCloudEvent> handler) + where TInput : class + where TOutput : class + { + return hostBuilder + .ConfigureSingleOutputService() + .ConfigureServices((_, services) => + { + services.AddTransient>(_ => + new AnonymousSingleOutputService(handler) + ); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousSingleOutputService( + this IMotorHostBuilder hostBuilder, + Func, TDep1, MotorCloudEvent> handler) + where TInput : class + where TOutput : class + where TDep1 : class + { + return hostBuilder + .ConfigureSingleOutputService() + .ConfigureServices((_, services) => + { + services.AddTransient>( + provider => + new AnonymousSingleOutputService(cloudEvent => + handler(cloudEvent, provider.GetRequiredService()) + ) + ); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousSingleOutputService( + this IMotorHostBuilder hostBuilder, + Func, TDep1, TDep2, MotorCloudEvent> handler) + where TInput : class + where TOutput : class + where TDep1 : class + where TDep2 : class + { + return hostBuilder + .ConfigureSingleOutputService() + .ConfigureServices((_, services) => + { + services.AddTransient>( + provider => + new AnonymousSingleOutputService(cloudEvent => + handler(cloudEvent, provider.GetRequiredService(), provider.GetRequiredService()) + ) + ); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousSingleOutputService( + this IMotorHostBuilder hostBuilder, + Func, TDep1, TDep2, TDep3, MotorCloudEvent> handler) + where TInput : class + where TOutput : class + where TDep1 : class + where TDep2 : class + where TDep3 : class + { + return hostBuilder + .ConfigureSingleOutputService() + .ConfigureServices((_, services) => + { + services.AddTransient>( + provider => + new AnonymousSingleOutputService(cloudEvent => + handler(cloudEvent, provider.GetRequiredService(), provider.GetRequiredService(), provider.GetRequiredService()) + ) + ); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousSingleOutputService( + this IMotorHostBuilder hostBuilder, + Func, CancellationToken, Task>> handler) + where TInput : class + where TOutput : class + { + return hostBuilder + .ConfigureSingleOutputService() + .ConfigureServices((_, services) => + { + services.AddTransient>(_ => + new AnonymousSingleOutputService(handler)); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousSingleOutputService( + this IMotorHostBuilder hostBuilder, + Func, TDep1, CancellationToken, Task>> handler) + where TInput : class + where TOutput : class + where TDep1 : class + { + return hostBuilder + .ConfigureSingleOutputService() + .ConfigureServices((_, services) => + { + services.AddTransient>(provider => + new AnonymousSingleOutputService((cloudEvent, token) => + handler(cloudEvent, provider.GetRequiredService(), token))); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousSingleOutputService( + this IMotorHostBuilder hostBuilder, + Func, TDep1, TDep2, CancellationToken, Task>> handler) + where TInput : class + where TOutput : class + where TDep1 : class + where TDep2 : class + { + return hostBuilder + .ConfigureSingleOutputService() + .ConfigureServices((_, services) => + { + services.AddTransient>(provider => + new AnonymousSingleOutputService((cloudEvent, token) => + handler(cloudEvent, provider.GetRequiredService(), provider.GetRequiredService(), token))); + }); + } + + public static IMotorHostBuilder ConfigureAnonymousSingleOutputService( + this IMotorHostBuilder hostBuilder, + Func, TDep1, TDep2, TDep3, CancellationToken, Task>> + handler) where TInput : class + where TOutput : class + where TDep1 : class + where TDep2 : class + where TDep3 : class + { + return hostBuilder + .ConfigureSingleOutputService() + .ConfigureServices((_, services) => + { + services.AddTransient>(provider => + new AnonymousSingleOutputService((cloudEvent, token) => + handler(cloudEvent, provider.GetRequiredService(), provider.GetRequiredService(), + provider.GetRequiredService(), token))); + }); + } + } +} diff --git a/src/Motor.Extensions.Utilities/DefaultHostBuilderExtensions.cs b/src/Motor.Extensions.Utilities/DefaultHostBuilderExtensions.cs index de25ba04d..239e70ddf 100644 --- a/src/Motor.Extensions.Utilities/DefaultHostBuilderExtensions.cs +++ b/src/Motor.Extensions.Utilities/DefaultHostBuilderExtensions.cs @@ -34,7 +34,7 @@ public static IMotorHostBuilder ConfigureSingleOutputService( { return hostBuilder .ConfigureNoOutputService() - .ConfigureServices((hostContext, services) => + .ConfigureServices((_, services) => { services.AddTransient, SingleOutputServiceAdapter>(); }); @@ -62,10 +62,8 @@ public static IMotorHostBuilder ConfigureNoOutputService(this IMotorHost { services.AddQueuedGenericService(); services.AddTransient, TracingDelegatingMessageHandler>(); - services.Configure( - hostContext.Configuration.GetSection(healthCheckConfigSection)); - services - .AddTransient, PrometheusDelegatingMessageHandler>(); + services.Configure(hostContext.Configuration.GetSection(healthCheckConfigSection)); + services.AddTransient, PrometheusDelegatingMessageHandler>(); }); } }