feat(client): add MessagingServiceClient and dependency injection extensions

Introduce the Email contract and MessagingServiceClient that send mail through the Application layer via MediatR, and add AddMessagingServiceClient overloads for delegate- and IConfiguration-based setup. Remove the empty client DI placeholders.
This commit is contained in:
2026-11-04 14:53:17 +01:00
parent eed9a0e915
commit 034d81f7f6
5 changed files with 118 additions and 20 deletions

View File

@@ -1,10 +0,0 @@
#if NET
namespace DigitalData.MessagingService.Client.DependencyInjection;
/// <summary>
///
/// </summary>
public static class DependencyInjectionExtensions
{
}
#endif

View File

@@ -1,10 +0,0 @@
#if NET
namespace DigitalData.MessagingService.Client.DependencyInjection;
/// <summary>
///
/// </summary>
public class MessagingServiceConfiguration
{
}
#endif

View File

@@ -0,0 +1,50 @@
#if NET
using DigitalData.MessagingService.Application.DependencyInjection;
using DigitalData.MessagingService.Publisher;
using DigitalData.MessagingService.RabbitMQ;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace DigitalData.MessagingService.Client.DependencyInjection;
/// <summary>
///
/// </summary>
public static class DependencyInjectionExtensions
{
private static IServiceCollection AddMessagingServiceClientDefaults(this IServiceCollection services, string luckyPennySoftLicenseKey)
{
services.AddMessagingServiceApplication(luckyPennySoftLicenseKey);
return services.AddTransient<MessagingServiceClient>();
}
/// <summary>
///
/// </summary>
/// <param name="services"></param>
/// <param name="luckyPennySoftLicenseKey"></param>
/// <param name="configureRabbitMqConnection"></param>
/// <returns></returns>
public static IServiceCollection AddMessagingServiceClient(this IServiceCollection services, string luckyPennySoftLicenseKey, Action<RabbitMqConnectionConfiguration> configureRabbitMqConnection)
{
services.AddMessagingServiceClientDefaults(luckyPennySoftLicenseKey);
services.AddMessagingServicePublisher(configureRabbitMqConnection);
return services;
}
/// <summary>
///
/// </summary>
/// <param name="services"></param>
/// <param name="luckyPennySoftLicenseKey"></param>
/// <param name="configuration"></param>
/// <param name="rabbitMqConnectionSectionName"></param>
/// <returns></returns>
public static IServiceCollection AddMessagingServiceClient(this IServiceCollection services, string luckyPennySoftLicenseKey, IConfiguration configuration, string rabbitMqConnectionSectionName = RabbitMqConnectionConfiguration.SectionName)
{
services.AddMessagingServiceClientDefaults(luckyPennySoftLicenseKey);
services.AddMessagingServicePublisher(configuration, rabbitMqConnectionSectionName);
return services;
}
}
#endif

View File

@@ -0,0 +1,34 @@
#if NET
namespace DigitalData.MessagingService.Client;
/// <summary>
/// Represents an email to be sent via the messaging service.
/// </summary>
public record Email
{
/// <summary>
/// Sender email address.
/// </summary>
public required string From { get; init; }
/// <summary>
/// Email subject.
/// </summary>
public required string Subject { get; init; }
/// <summary>
/// Email body (HTML or plain text).
/// </summary>
public required string Body { get; init; }
/// <summary>
/// Indicates whether <see cref="Body"/> is HTML.
/// </summary>
public required bool IsHtml { get; init; }
/// <summary>
/// Recipient email addresses.
/// </summary>
public required List<string> To { get; init; }
}
#endif

View File

@@ -0,0 +1,34 @@
#if NET
using DigitalData.MessagingService.Application.EmailAccounts.Queries;
using DigitalData.MessagingService.Application.EmailSending.Commands;
using MediatR;
namespace DigitalData.MessagingService.Client;
/// <summary>
///
/// </summary>
public class MessagingServiceClient(ISender sender)
{
/// <summary>
///
/// </summary>
/// <param name="email"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task SendAsync(Email email, CancellationToken cancellationToken = default)
{
await sender.Send(new PublishEmailViaSmtpCommand()
{
Sender = new GetEmailAccountQuery()
{
Username = email.From
},
Recipients = email.To,
Subject = email.Subject,
Body = email.Body,
IsHtml = email.IsHtml
}, cancellationToken);
}
}
#endif