我在我的项目中使用 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
如果它不打算直接在我的代码中使用,我应该使用什么?
我的应用程序还在其他部分发送电子邮件,不仅仅是身份。
最佳答案
我认为您不需要使用 IEmailSender
.事实上,根据我的经验,在某种程度上我认为你不应该使用它:
IEmailSender
在包裹中 ASP.NET Core Identity.UI
,与 default implementation注入(inject)到什么都不做的 DI 中。如果您想使用框架提供的默认 UI,那完全没问题。但是如果你不喜欢使用那些默认页面,只想使用Identity框架的核心,包括整个包只是为了得到IEmailSender
界面对我来说似乎有点矫枉过正。更何况还有an annoying bug此时使用身份 UI 时。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/