UI 项目之外的 IEmailSender

IEmailSender outside of the UI project

使用RazorUI处理注册时,需要实现IEmailSender并在startup.cs中注册。有没有办法在解决方案中的另一个 class 库项目中使用邮件 class?邮件 class 是我希望远离 UI 项目的东西。

我可能会在 class 库中安装 Microsoft.AspNetCore.Identity.UI 的 nuget 包,但它附带了大约 50 个依赖项。有没有更好、更轻量级的方法?

我刚刚度过了一个漫长的夜晚,但我并没有得到答案。关于包装外部服务的评论中提出了一个很好的观点,所以我已经做到了。

这里的 EmailSender class 只是包装了我的 IMailService,它是在 class 库项目中实现的。

public class EmailSender : IEmailSender
{
    private readonly IMailService mailService;

    public EmailSender(IMailService mailService)
    {
        this.mailService = mailService ?? throw new ArgumentNullException(nameof(mailService));
    }

    public Task SendEmailAsync(string email, string subject, string htmlMessage)
    {
        return Task.Run(() => mailService.Send(new string[] { email }, subject, htmlMessage));
    }
}