From 907a4453cc473e7712c4df52c3b218b6a08e5427 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Wed, 18 Jan 2023 02:12:35 +0200 Subject: [PATCH] Add messaging --- .gitignore | 4 +- Correo.sln | 14 ++++++ dependencies.props | 6 +-- .../Correo.Application.csproj | 11 +++++ .../Queries/GetSystemVersion.cs | 39 ++++++++++++++++ src/Correo.PublishedLanguage/Class1.cs | 7 +++ .../Correo.PublishedLanguage.csproj | 11 +++++ src/Correo/Correo.csproj | 15 +++++-- src/Correo/Extensions/MessagingExtensions.cs | 45 +++++++++++++++++++ src/Correo/Extensions/StartupExtensions.cs | 16 +++++-- .../SubscriberLoggingMiddleware.cs | 25 +++++++++++ src/Correo/appsettings.json | 16 ++++++- 12 files changed, 194 insertions(+), 15 deletions(-) create mode 100644 src/Correo.Application/Correo.Application.csproj create mode 100644 src/Correo.Application/Queries/GetSystemVersion.cs create mode 100644 src/Correo.PublishedLanguage/Class1.cs create mode 100644 src/Correo.PublishedLanguage/Correo.PublishedLanguage.csproj create mode 100644 src/Correo/Extensions/MessagingExtensions.cs create mode 100644 src/Correo/Middlewares/SubscriberLoggingMiddleware.cs diff --git a/.gitignore b/.gitignore index 9491a2f..32b26c1 100644 --- a/.gitignore +++ b/.gitignore @@ -360,4 +360,6 @@ MigrationBackup/ .ionide/ # Fody - auto-generated XML schema -FodyWeavers.xsd \ No newline at end of file +FodyWeavers.xsd + +*/**/appsettings.Development.json \ No newline at end of file diff --git a/Correo.sln b/Correo.sln index 017368f..1c5cffa 100644 --- a/Correo.sln +++ b/Correo.sln @@ -18,6 +18,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solution items", "solution README.md = README.md EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Correo.Application", "src\Correo.Application\Correo.Application.csproj", "{3AEA1AB4-F068-4C39-A173-AD65F3F8E2F2}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Correo.PublishedLanguage", "src\Correo.PublishedLanguage\Correo.PublishedLanguage.csproj", "{DA47BCEE-9AE7-4F20-A04F-5866966503C5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -28,12 +32,22 @@ Global {B712E585-E63F-4A6D-8E17-FEFACE04BBE2}.Debug|Any CPU.Build.0 = Debug|Any CPU {B712E585-E63F-4A6D-8E17-FEFACE04BBE2}.Release|Any CPU.ActiveCfg = Release|Any CPU {B712E585-E63F-4A6D-8E17-FEFACE04BBE2}.Release|Any CPU.Build.0 = Release|Any CPU + {3AEA1AB4-F068-4C39-A173-AD65F3F8E2F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3AEA1AB4-F068-4C39-A173-AD65F3F8E2F2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3AEA1AB4-F068-4C39-A173-AD65F3F8E2F2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3AEA1AB4-F068-4C39-A173-AD65F3F8E2F2}.Release|Any CPU.Build.0 = Release|Any CPU + {DA47BCEE-9AE7-4F20-A04F-5866966503C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DA47BCEE-9AE7-4F20-A04F-5866966503C5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DA47BCEE-9AE7-4F20-A04F-5866966503C5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DA47BCEE-9AE7-4F20-A04F-5866966503C5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {B712E585-E63F-4A6D-8E17-FEFACE04BBE2} = {245E2FBE-DFDF-40B4-94B7-5DDA216E58AD} + {3AEA1AB4-F068-4C39-A173-AD65F3F8E2F2} = {245E2FBE-DFDF-40B4-94B7-5DDA216E58AD} + {DA47BCEE-9AE7-4F20-A04F-5866966503C5} = {245E2FBE-DFDF-40B4-94B7-5DDA216E58AD} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {86FCF989-26FC-41E9-8A23-9485606D619D} diff --git a/dependencies.props b/dependencies.props index 1249227..ab137b6 100644 --- a/dependencies.props +++ b/dependencies.props @@ -2,11 +2,9 @@ 6.0.0 6.1.0 - 4.0.1 5.5.0 - 10.1.1 - 8.1.1 + 6.2.3 9.0.0 - 1.0.6 + 6.0.30 \ No newline at end of file diff --git a/src/Correo.Application/Correo.Application.csproj b/src/Correo.Application/Correo.Application.csproj new file mode 100644 index 0000000..9c6904d --- /dev/null +++ b/src/Correo.Application/Correo.Application.csproj @@ -0,0 +1,11 @@ + + + + net6.0 + + + + + + + diff --git a/src/Correo.Application/Queries/GetSystemVersion.cs b/src/Correo.Application/Queries/GetSystemVersion.cs new file mode 100644 index 0000000..e98f839 --- /dev/null +++ b/src/Correo.Application/Queries/GetSystemVersion.cs @@ -0,0 +1,39 @@ +using MediatR; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace Correo.Application.Queries +{ + public class GetSystemVersion + { + public record Query : IRequest + { + public int CountyId { get; init; } + } + + public record Model + { + public string Version { get; init; } + public DateTime LastUpdateDate { get; init; } + } + + public class QueryHandler : IRequestHandler + { + public QueryHandler() + { + } + + public async Task Handle(Query request, CancellationToken cancellationToken) + { + var result = new Model() + { + Version = "1.0.0", + LastUpdateDate = DateTime.Now + }; + + return await Task.FromResult(result); + } + } + } +} diff --git a/src/Correo.PublishedLanguage/Class1.cs b/src/Correo.PublishedLanguage/Class1.cs new file mode 100644 index 0000000..d476516 --- /dev/null +++ b/src/Correo.PublishedLanguage/Class1.cs @@ -0,0 +1,7 @@ +namespace Correo.PublishedLanguage +{ + public class Class1 + { + + } +} \ No newline at end of file diff --git a/src/Correo.PublishedLanguage/Correo.PublishedLanguage.csproj b/src/Correo.PublishedLanguage/Correo.PublishedLanguage.csproj new file mode 100644 index 0000000..edc9169 --- /dev/null +++ b/src/Correo.PublishedLanguage/Correo.PublishedLanguage.csproj @@ -0,0 +1,11 @@ + + + + net6.0 + + + + + + + diff --git a/src/Correo/Correo.csproj b/src/Correo/Correo.csproj index e20cee5..90cdbcc 100644 --- a/src/Correo/Correo.csproj +++ b/src/Correo/Correo.csproj @@ -1,13 +1,20 @@ - + net6.0 - - - + + + + + + + + + + diff --git a/src/Correo/Extensions/MessagingExtensions.cs b/src/Correo/Extensions/MessagingExtensions.cs new file mode 100644 index 0000000..a70d379 --- /dev/null +++ b/src/Correo/Extensions/MessagingExtensions.cs @@ -0,0 +1,45 @@ +using Correo.Middlewares; +using MediatR; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using NBB.Messaging.Host; + +namespace Correo.Extensions +{ + public static class MessagingExtensions + { + public static void AddMessaging(this IServiceCollection services, IConfiguration configuration) + { + var enabled = configuration.GetValue("Messaging:Enabled", false); + if (!enabled) + return; + + services.AddMessageBus(configuration); + services.AddMessagingHost(configuration); + } + + private static void AddMessageBus(this IServiceCollection services, IConfiguration configuration) + { + services.AddMessageBus().AddNatsTransport(configuration); + } + + private static void AddMessagingHost(this IServiceCollection services, IConfiguration configuration) + { + services.AddMessagingHost(configuration, hostBuilder => + { + hostBuilder + .Configure(configBuilder => configBuilder + .AddSubscriberServices(config => config + .FromMediatRHandledCommands().AddClassesAssignableTo() + .FromMediatRHandledEvents().AddClassesAssignableTo()) + .WithDefaultOptions() + .UsePipeline(builder => builder + .UseCorrelationMiddleware() + .UseExceptionHandlingMiddleware() + .UseDefaultResiliencyMiddleware() + .UseMiddleware() + .UseMediatRMiddleware())); + }); + } + } +} diff --git a/src/Correo/Extensions/StartupExtensions.cs b/src/Correo/Extensions/StartupExtensions.cs index a2f1637..8ea74b4 100644 --- a/src/Correo/Extensions/StartupExtensions.cs +++ b/src/Correo/Extensions/StartupExtensions.cs @@ -1,4 +1,6 @@ -using Microsoft.AspNetCore.Builder; +using MediatR; +using MediatR.Pipeline; +using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -10,16 +12,24 @@ namespace Correo.Extensions public static void ConfigureServices(this IServiceCollection services, IConfiguration configuration) { // Add services to the container. - services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle services.AddEndpointsApiExplorer(); services.AddSwaggerGen(); + + // MediatR + services.AddMediatR(typeof(Application.Queries.GetSystemVersion).Assembly); + services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>)); + + // Messaging + services.AddMessaging(configuration); } public static void Configure(this WebApplication app) { + app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()); + // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { @@ -28,8 +38,6 @@ namespace Correo.Extensions } app.UseAuthorization(); - - app.MapControllers(); } } diff --git a/src/Correo/Middlewares/SubscriberLoggingMiddleware.cs b/src/Correo/Middlewares/SubscriberLoggingMiddleware.cs new file mode 100644 index 0000000..d6a9cd5 --- /dev/null +++ b/src/Correo/Middlewares/SubscriberLoggingMiddleware.cs @@ -0,0 +1,25 @@ +using Microsoft.Extensions.Logging; +using System.Threading.Tasks; +using System.Threading; +using System; +using NBB.Messaging.Abstractions; +using NBB.Core.Pipeline; + +namespace Correo.Middlewares +{ + public class SubscriberLoggingMiddleware : IPipelineMiddleware + { + private readonly ILogger _logger; + + public SubscriberLoggingMiddleware(ILogger logger) + { + _logger = logger; + } + + public async Task Invoke(MessagingContext ctx, CancellationToken cancellationToken, Func next) + { + _logger.LogDebug("Message {@Message} was received.", ctx.MessagingEnvelope.Payload); + await next(); + } + } +} diff --git a/src/Correo/appsettings.json b/src/Correo/appsettings.json index aa5c599..308d313 100644 --- a/src/Correo/appsettings.json +++ b/src/Correo/appsettings.json @@ -6,11 +6,23 @@ }, "Serilog": { "MinimumLevel": { - "Default": "Debug", + "Default": "Warning", "Override": { "Microsoft": "Information" } } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "Messaging": { + "Enabled": false, + "TopicPrefix": "HomeLab.", + "Source": "Correo", + "Nats": { + "natsUrl": "nats://:4222", + "cluster": "", + "clientId": "Correo", + "qGroup": "Correo", + "durableName": "durable" + } + } }