c# - 使用 IOption 的类的实例化

标签 c# asp.net-mvc asp.net-core dependency-injection .net-core

在 ASP.NET Core 2.1 中,我有一个使用 IOptions 接口(interface)发送电子邮件的类,如下所示:

public class Email
{
    private readonly ManuelaIbiEmail manuelaIbiEmail;

    public Email(IOptions<ManuelaIbiEmail> manuelaIbiEmail)
    {
        this.manuelaIbiEmail.Username = manuelaIbiEmail.Value.Username;
        this.manuelaIbiEmail.Password = manuelaIbiEmail.Value.Password;
    }

    public async Task SendAsync(Contato contato)
    {
        var smtpClient = new SmtpClient
        {
            Host = "smtp.sendgrid.net",
            Port = 587,
            EnableSsl = true,
            Credentials = new NetworkCredential(manuelaIbiEmail.Username, manuelaIbiEmail.Password)
        };

        using (var message = new MailMessage(contato.Email, "manuelaibi66@gmail.com")
        {
            Subject = "Email de Manuela Ibi Nutrição Integrada",
            Body = $"{contato.Comentario}\nTelefone: {contato.Telefone}"
        })
        {
            await smtpClient.SendMailAsync(message);
        }
    }
}

这个类必须在 Controller 中用类似“var whatever = new Email(???);”这样的行来实例化。以便可以调用 SendAsync 方法。

但这里的大局是我迷失在这个初始化过程中。我只是不知道在“Email(something)”中调用什么。

只是,提供更多信息以备不时之需:

系统应该从 Secrets 中读取用户名和密码,因此我在 Startup.cs 中有以下行:

services.Configure<ManuelaIbiEmail>(Configuration.GetSection("ManuelaIbiEmail"));

ManuelaIbiEmail 是一个非常简单的 POCO 类:

public class ManuelaIbiEmail
{
    public string Username { get; set; }
    public string Password { get; set; }
}

Controller 看起来像:

public class ContactController : Controller
{
    private readonly ManuelaIbiEmail manuelaIbiEmail;

    public IActionResult Contact()
    {
        return View(new Contato());
    }

    [HttpPost]
    public async Task<IActionResult> ContactAsync(Contato contato)
    {
        var email = new Email();
        email.SendAsync(contato);

        return Ok();
    }
}

如果有人能好心告诉我我缺少什么,我将不胜感激。

提前致谢

最佳答案

依赖注入(inject)的重点是不要那样做

您可以通过属性在方法中注入(inject)它:

public class ContactController : Controller
{
    public IActionResult Contact()
    {
        return View(new Contato());
    }

    [HttpPost]
    public async Task<IActionResult> ContactAsync(Contato contato, [FromServices]Email email)
    {
        email.SendAsync(contato);

        return Ok();
    }
}

或者通过构造函数注入(inject):

public class ContactController : Controller
{
    private readonly EMail email;

    public ContactController(Email email)
    {
        this.email = email;
    }

    public IActionResult Contact()
    {
        return View(new Contato());
    }

    [HttpPost]
    public async Task<IActionResult> ContactAsync(Contato contato)
    {
        email.SendAsync(contato);

        return Ok();
    }
}

关于c# - 使用 IOption 的类的实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52166688/

相关文章:

c# - 通过代码在 Windows 10 上启用平板电脑模式?

asp.net - MVC4中Dropdownlist的某些记录设置类属性

c# - 异步和线程文化

powershell - 错误: Need to specify the physical directory for the virtual path 'Web/' of role

c# - 无效的 URI : The format of the URI could not be determined

C#更新数据库

c# - 将焦点放回主窗体

javascript - JQuery 未在我的 Controller 上调用 HttpPost 方法。我该如何解决这个问题,或者可能出了什么问题?

c# - 集成测试给出编译错误 CS0051

c# - 如何在asp.net core中修改HttpContext.Request.Form