Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve DX regarding service collection extension methods #28

Merged
merged 4 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Unreleased

- Added some badges to the README
- Marked `AddRazorEmailRenderer` obsolete, use `AddRazorMailRenderer` instead
chucker marked this conversation as resolved.
Show resolved Hide resolved
- Marked `AddMailKitRazorMailClient` obsolete, use `AddMailKitMailClient` instead
- Marked `AddSystemNetRazorMailClient` obsolete, use `AddSystemNetMailClient` instead
- Added `configureClient` parameter to `AddMailKitMailClient` and `AddSystemNetMailClient` so you can configure the
client instance (e.g. default headers)

# 2.1.1

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;

using Microsoft.Extensions.DependencyInjection;

using TRENZ.Lib.RazorMail.Interfaces;
Expand All @@ -7,13 +9,17 @@ namespace TRENZ.Lib.RazorMail.Extensions;

public static class RazorMailServiceCollectionExtensions
{
public static IServiceCollection AddRazorEmailRenderer(this IServiceCollection services)
[Obsolete("Use AddRazorMailRenderer instead.")]
public static IServiceCollection AddRazorEmailRenderer(this IServiceCollection services) =>
services.AddRazorMailRenderer();

public static IServiceCollection AddRazorMailRenderer(this IServiceCollection services)
{
services.AddMvcCore()
.AddRazorViewEngine();
.AddRazorViewEngine();

services.AddTransient<IMailRenderer, MailRenderer>();
services.AddTransient<IMailRenderer, RazorMailRenderer>();
chucker marked this conversation as resolved.
Show resolved Hide resolved

return services;
}
}
}
2 changes: 1 addition & 1 deletion TRENZ.Lib.RazorMail.Core/Services/BaseSmtpMailClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public abstract class BaseSmtpMailClient(IOptions<SmtpAccount> accountOptions) :
protected SmtpAccount Account => accountOptions.Value;

/// <inheritdoc />
public MailHeaderCollection DefaultHeaders { get; } = new();
public MailHeaderCollection DefaultHeaders { get; set; } = new();

/// <inheritdoc />
public Task SendAsync(MailMessage message, CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace TRENZ.Lib.RazorMail.Services;
/// <summary>
/// via https://scottsauber.com/2018/07/07/walkthrough-creating-an-html-email-template-with-razor-and-razor-class-libraries-and-rendering-it-from-a-net-standard-class-library/
/// </summary>
public class MailRenderer(
public class RazorMailRenderer(
[SuppressMessage("ReSharper", "SuggestBaseTypeForParameterInConstructor",
Justification = "Only IRazorViewEngine is registered in the DI container.")]
IRazorViewEngine viewEngine,
Expand Down Expand Up @@ -103,4 +103,4 @@ private ActionContext GetActionContext()

return new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

using TRENZ.Lib.RazorMail.Interfaces;
using TRENZ.Lib.RazorMail.Models;
Expand All @@ -7,19 +11,55 @@ namespace TRENZ.Lib.RazorMail.MailKit.Extensions;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddMailKitRazorMailClient(this IServiceCollection services)
[Obsolete("Use AddMailKitMailClient instead.")]
public static IServiceCollection AddMailKitRazorMailClient(this IServiceCollection services) =>
services.AddMailKitMailClient();

public static IServiceCollection AddMailKitMailClient(
this IServiceCollection services,
Action<IServiceProvider, MailKitMailClient>? configureClient = null
) => services.InternalAddMailKitMailClient(configureClient);

[Obsolete("Use AddMailKitMailClient instead.")]
public static IServiceCollection AddMailKitRazorMailClient(
this IServiceCollection services,
object serviceKey
) => services.InternalAddMailKitMailClient(null, serviceKey);

public static IServiceCollection AddMailKitMailClient(
this IServiceCollection services,
object serviceKey,
Action<IServiceProvider, MailKitMailClient>? configureClient = null
) => services.InternalAddMailKitMailClient(configureClient, serviceKey);

private static IServiceCollection InternalAddMailKitMailClient(
this IServiceCollection services,
Action<IServiceProvider, MailKitMailClient>? configureClient = null,
object? serviceKey = null
)
{
services.AddOptions<SmtpAccount>().BindConfiguration(SmtpAccount.SectionName);
services.AddSingleton<IMailClient, MailKitMailClient>();

if (serviceKey is null)
services.AddSingleton<IMailClient, MailKitMailClient>(sp =>
CreateMailKitMailClient(sp, configureClient));
else
services.AddKeyedSingleton<IMailClient, MailKitMailClient>(serviceKey,
(sp, _) => CreateMailKitMailClient(sp, configureClient));

return services;
}

public static IServiceCollection AddMailKitRazorMailClient(this IServiceCollection services, object? key)
private static MailKitMailClient CreateMailKitMailClient(IServiceProvider sp,
Action<IServiceProvider, MailKitMailClient>? configureClient)
{
services.AddOptions<SmtpAccount>().BindConfiguration(SmtpAccount.SectionName);
services.AddKeyedSingleton<IMailClient, MailKitMailClient>(key);
var accountOptions = sp.GetRequiredService<IOptions<SmtpAccount>>();
var logger = sp.GetRequiredService<ILogger<MailKitMailClient>>();

return services;
var client = new MailKitMailClient(accountOptions, logger);

configureClient?.Invoke(sp, client);

return client;
}
}
15 changes: 12 additions & 3 deletions TRENZ.Lib.RazorMail.SampleWebApi/Program.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
using TRENZ.Lib.RazorMail.Extensions;
using TRENZ.Lib.RazorMail.Interfaces;
using TRENZ.Lib.RazorMail.MailKit.Extensions;
using TRENZ.Lib.RazorMail.Models;
using TRENZ.Lib.RazorMail.SystemNet.Extensions;

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddJsonFile("appsettings.local.json", optional: true);

builder.Services.AddRazorEmailRenderer();
builder.Services.AddMailKitRazorMailClient("MailKit");
builder.Services.AddSystemNetRazorMailClient("System.Net.Mail");
builder.Services.AddRazorMailRenderer();
builder.Services.AddMailKitMailClient("MailKit", ConfigureClient);
builder.Services.AddSystemNetMailClient("System.Net.Mail", ConfigureClient);

var app = builder.Build();

app.MapControllers();

app.Run();

return;

static void ConfigureClient(IServiceProvider sp, IMailClient client)
{
client.DefaultHeaders.ReplyTo = [new MailAddress("[email protected]")];
}
2 changes: 1 addition & 1 deletion TRENZ.Lib.RazorMail.SampleWebApi/sendsamplemail.http
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Via HTTPS and System.Net.Mail
POST https://localhost:7141/mail/SendWithSystemNet
POST http://localhost:5102/mail/SendWithSystemNet
Content-Type: application/json

{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

using TRENZ.Lib.RazorMail.Interfaces;
using TRENZ.Lib.RazorMail.Models;
Expand All @@ -7,19 +11,56 @@ namespace TRENZ.Lib.RazorMail.SystemNet.Extensions;

public static class ServiceCollectionExtensions
{
[Obsolete("Use AddSystemNetMailClient instead.")]
public static IServiceCollection AddSystemNetRazorMailClient(this IServiceCollection services)
=> services.AddSystemNetMailClient();

public static IServiceCollection AddSystemNetMailClient(
this IServiceCollection services,
Action<IServiceProvider, SystemNetMailClient>? configureClient = null
) => services.InternalAddSystemNetMailClient(configureClient);


[Obsolete("Use AddSystemNetMailClient instead.")]
public static IServiceCollection AddSystemNetRazorMailClient(
this IServiceCollection services,
object serviceKey
) => services.InternalAddSystemNetMailClient(null, serviceKey);

public static IServiceCollection AddSystemNetMailClient(
this IServiceCollection services,
object serviceKey,
Action<IServiceProvider, SystemNetMailClient>? configureClient = null
) => services.InternalAddSystemNetMailClient(configureClient, serviceKey);

private static IServiceCollection InternalAddSystemNetMailClient(
this IServiceCollection services,
Action<IServiceProvider, SystemNetMailClient>? configureClient = null,
object? serviceKey = null
)
{
services.AddOptions<SmtpAccount>().BindConfiguration(SmtpAccount.SectionName);
services.AddSingleton<IMailClient, SystemNetMailClient>();

if (serviceKey is null)
services.AddSingleton<IMailClient, SystemNetMailClient>(sp =>
CreateSystemNetMailClient(sp, configureClient));
else
services.AddKeyedSingleton<IMailClient, SystemNetMailClient>(serviceKey,
(sp, _) => CreateSystemNetMailClient(sp, configureClient));

return services;
}

public static IServiceCollection AddSystemNetRazorMailClient(this IServiceCollection services, object? key)
private static SystemNetMailClient CreateSystemNetMailClient(IServiceProvider sp,
Action<IServiceProvider, SystemNetMailClient>? configureClient)
{
services.AddOptions<SmtpAccount>().BindConfiguration(SmtpAccount.SectionName);
services.AddKeyedSingleton<IMailClient, SystemNetMailClient>(key);
var accountOptions = sp.GetRequiredService<IOptions<SmtpAccount>>();
var logger = sp.GetRequiredService<ILogger<SystemNetMailClient>>();

return services;
var client = new SystemNetMailClient(accountOptions, logger);

configureClient?.Invoke(sp, client);

return client;
}
}