asp.net-core - 我应该使用 IEmailSender 吗?

标签 asp.net-core interface asp.net-identity

我在我的项目中使用 AspNetCore.Identity,因此我编写了一个实现此接口(interface)的 EmailSender 类。

一切正常。

但是这个描述是关于IEmailSender引起了我的注意:

Microsoft.AspNetCore.Identity.UI.Services.IEmailSender
This API supports the ASP.NET Core Identity default UI infraestructure and is not intended to be used directly from your code. This API may change or be removed in future releases



enter image description here

如果它不打算直接在我的代码中使用,我应该使用什么?
我的应用程序还在其他部分发送电子邮件,不仅仅是身份。

最佳答案

我认为您不需要使用 IEmailSender .事实上,根据我的经验,在某种程度上我认为你不应该使用它:

  • 在 .NET 5.0 中,IEmailSender在包裹中 ASP.NET Core Identity.UI ,与 default implementation注入(inject)到什么都不做的 DI 中。如果您想使用框架提供的默认 UI,那完全没问题。但是如果你不喜欢使用那些默认页面,只想使用Identity框架的核心,包括整个包只是为了得到IEmailSender界面对我来说似乎有点矫枉过正。更何况还有an annoying bug此时使用身份 UI 时。
  • 它不会增加任何值(value)。如果框架可以自动扫描我的具体实现并使用它来发送电子邮件,我会被说服。但事实并非如此。您必须通过 services.AddSingleton<>() 通过 DI 注入(inject)您的电子邮件发件人自己并通过构造函数注入(inject)使用它来发送电子邮件。
  • IEmailSender.SendEmailAsync(string email, string subject, string htmlMessage)方法很基本。大多数情况下,您甚至必须创建自己的 razor 库来自己生成 HTML 消息。在我的情况下(使用带有动态模板的 SendGrid v9.22),我不仅需要传递 HTML 消息,还需要传递模板数据(用于通过 msg.SetTemplateData() 在我的 SendGrid 模板中设置占位符)。基本SendEmailAsync()只是没有额外的参数让我在发送电子邮件时发送额外的数据。

  • 结论:
    如果您发现自己不会为某个项目切换到电子邮件提供商,那么您不妨创建一个适合您的具体电子邮件发件人,并将其直接注入(inject) DI。
    如果你看到你的项目可能会切换电子邮件提供商,有一个基本和简单的界面,比如 IEmailSender , 说得通。

    关于asp.net-core - 我应该使用 IEmailSender 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61636393/

    相关文章:

    asp.net-core - ASP.NET Core MVC - 模型绑定(bind) : Bind an interface model using the attribute [FromBody] (BodyModelBinder)

    java - 如何获取存储在集合中的对象的特定方法?

    java - 服务器启动后界面卡住

    github - ASP.NET Core UserManager FindByNameAsync 数据库调用或从内存

    c# - 从 Web API 操作中的请求中检索 token 值

    asp.net - 为什么ASP.NET Identity中的注销使用POST而不是GET?

    azure - Azure Linux Web 应用程序上的 ASP.NET Core NodeServices

    AWS Linux 中的 ASP.NET 异常

    docker - 如何定义Ocelot API网关的URL

    oop - 何时实现接口(interface)以及何时扩展父类(super class)?