Add messaging

master
Tudor Stanciu 2023-01-18 02:12:35 +02:00
parent 2377370aab
commit 907a4453cc
12 changed files with 194 additions and 15 deletions

4
.gitignore vendored
View File

@ -360,4 +360,6 @@ MigrationBackup/
.ionide/ .ionide/
# Fody - auto-generated XML schema # Fody - auto-generated XML schema
FodyWeavers.xsd FodyWeavers.xsd
*/**/appsettings.Development.json

View File

@ -18,6 +18,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solution items", "solution
README.md = README.md README.md = README.md
EndProjectSection EndProjectSection
EndProject 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 Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{B712E585-E63F-4A6D-8E17-FEFACE04BBE2}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
EndGlobalSection EndGlobalSection
GlobalSection(NestedProjects) = preSolution GlobalSection(NestedProjects) = preSolution
{B712E585-E63F-4A6D-8E17-FEFACE04BBE2} = {245E2FBE-DFDF-40B4-94B7-5DDA216E58AD} {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 EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {86FCF989-26FC-41E9-8A23-9485606D619D} SolutionGuid = {86FCF989-26FC-41E9-8A23-9485606D619D}

View File

@ -2,11 +2,9 @@
<PropertyGroup Label="Package Versions"> <PropertyGroup Label="Package Versions">
<MicrosoftExtensionsPackageVersion>6.0.0</MicrosoftExtensionsPackageVersion> <MicrosoftExtensionsPackageVersion>6.0.0</MicrosoftExtensionsPackageVersion>
<SerilogPackageVersion>6.1.0</SerilogPackageVersion> <SerilogPackageVersion>6.1.0</SerilogPackageVersion>
<SerilogSinksConsolePackageVersion>4.0.1</SerilogSinksConsolePackageVersion>
<SerilogSinksSQLitePackageVersion>5.5.0</SerilogSinksSQLitePackageVersion> <SerilogSinksSQLitePackageVersion>5.5.0</SerilogSinksSQLitePackageVersion>
<AutoMapperPackageVersion>10.1.1</AutoMapperPackageVersion> <SwashbucklePackageVersion>6.2.3</SwashbucklePackageVersion>
<AutoMapperExtensionsPackageVersion>8.1.1</AutoMapperExtensionsPackageVersion>
<MediatRPackageVersion>9.0.0</MediatRPackageVersion> <MediatRPackageVersion>9.0.0</MediatRPackageVersion>
<NetmashExtensionsSwaggerPackageVersion>1.0.6</NetmashExtensionsSwaggerPackageVersion> <NBBPackageVersion>6.0.30</NBBPackageVersion>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Correo.PublishedLanguage\Correo.PublishedLanguage.csproj" />
</ItemGroup>
</Project>

View File

@ -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<Model>
{
public int CountyId { get; init; }
}
public record Model
{
public string Version { get; init; }
public DateTime LastUpdateDate { get; init; }
}
public class QueryHandler : IRequestHandler<Query, Model>
{
public QueryHandler()
{
}
public async Task<Model> Handle(Query request, CancellationToken cancellationToken)
{
var result = new Model()
{
Version = "1.0.0",
LastUpdateDate = DateTime.Now
};
return await Task.FromResult(result);
}
}
}
}

View File

@ -0,0 +1,7 @@
namespace Correo.PublishedLanguage
{
public class Class1
{
}
}

View File

@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MediatR" Version="$(MediatRPackageVersion)" />
</ItemGroup>
</Project>

View File

@ -1,13 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Serilog.AspNetCore" Version="6.1.0" /> <PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="$(MediatRPackageVersion)" />
<PackageReference Include="Serilog.Sinks.SQLite" Version="5.5.0" /> <PackageReference Include="NBB.Messaging.Host" Version="$(NBBPackageVersion)" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> <PackageReference Include="NBB.Messaging.Nats" Version="$(NBBPackageVersion)" />
<PackageReference Include="Serilog.AspNetCore" Version="$(SerilogPackageVersion)" />
<PackageReference Include="Serilog.Sinks.SQLite" Version="$(SerilogSinksSQLitePackageVersion)" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="$(SwashbucklePackageVersion)" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Correo.Application\Correo.Application.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -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<bool>("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<IRequest>()
.FromMediatRHandledEvents().AddClassesAssignableTo<INotification>())
.WithDefaultOptions()
.UsePipeline(builder => builder
.UseCorrelationMiddleware()
.UseExceptionHandlingMiddleware()
.UseDefaultResiliencyMiddleware()
.UseMiddleware<SubscriberLoggingMiddleware>()
.UseMediatRMiddleware()));
});
}
}
}

View File

@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Builder; using MediatR;
using MediatR.Pipeline;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
@ -10,16 +12,24 @@ namespace Correo.Extensions
public static void ConfigureServices(this IServiceCollection services, IConfiguration configuration) public static void ConfigureServices(this IServiceCollection services, IConfiguration configuration)
{ {
// Add services to the container. // Add services to the container.
services.AddControllers(); services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
services.AddEndpointsApiExplorer(); services.AddEndpointsApiExplorer();
services.AddSwaggerGen(); 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) public static void Configure(this WebApplication app)
{ {
app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())
{ {
@ -28,8 +38,6 @@ namespace Correo.Extensions
} }
app.UseAuthorization(); app.UseAuthorization();
app.MapControllers(); app.MapControllers();
} }
} }

View File

@ -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<MessagingContext>
{
private readonly ILogger<SubscriberLoggingMiddleware> _logger;
public SubscriberLoggingMiddleware(ILogger<SubscriberLoggingMiddleware> logger)
{
_logger = logger;
}
public async Task Invoke(MessagingContext ctx, CancellationToken cancellationToken, Func<Task> next)
{
_logger.LogDebug("Message {@Message} was received.", ctx.MessagingEnvelope.Payload);
await next();
}
}
}

View File

@ -6,11 +6,23 @@
}, },
"Serilog": { "Serilog": {
"MinimumLevel": { "MinimumLevel": {
"Default": "Debug", "Default": "Warning",
"Override": { "Override": {
"Microsoft": "Information" "Microsoft": "Information"
} }
} }
}, },
"AllowedHosts": "*" "AllowedHosts": "*",
"Messaging": {
"Enabled": false,
"TopicPrefix": "HomeLab.",
"Source": "Correo",
"Nats": {
"natsUrl": "nats://<host>:4222",
"cluster": "<cluster>",
"clientId": "Correo",
"qGroup": "Correo",
"durableName": "durable"
}
}
} }