Someone in our company wrote ( by mistake, a long time ago ) this static class, being served from Nuget : (I can't edit it)
public static class MailService
{
public static void SendEmail(string recipients, string subject, string msg )
{
SendEmail(recipients, subject, msg);
}
...
}
I already know it's bad. But let's continue.
I want to use this class via DI in my asp.net core project.
Problem is that I can't use this (because static
can't be new'ed):
services.AddSingleton<MailService>();
Question:
Is there still a way to make MailService
(or any wrapper for it ) Injectable?
It's as simple as this:
public interface IMailService {
void SendEmail(string recipients, string subject, string msg);
}
public class MailServiceWrapper : IMailService {
public void SendEmail(string recipients, string subject, string msg) {
MailService.SendEmail(recipients, subject, msg);
}
}
And in the Startup class:
services.AddSingleton<IMailService, MailServiceWrapper>();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.