我正在使用ASP.net核心开发Web应用程序,这是我的新技术。
我正在使用一种服务来发送电子邮件,这是课程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using System.Net.Mail;
namespace WebApplication.Services
{
public class MessageSender : IEmailSender, ISmsSender
{
public MessageSender(IOptions<MessageSenderOptions> optionsAccessor)
{
Options = optionsAccessor.Value;
}
public MessageSenderOptions Options { get; } //set only via Secret Manager
public Task SendEmailAsync(string email, string subject, string message)
{
// Plug in your email service here to send an email.
using (var smtp = new SmtpClient(Options.emailServer))
{
var mail = new MailMessage
{
Subject = subject,
IsBodyHtml = true,
From = new MailAddress(Options.fromEmail, Options.fromName),
Body = message
};
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") != "Development")
{
mail.To.Add(email);
}
else
{
mail.To.Add("test@gmail.com");
}
smtp.Send(mail);
}
return Task.FromResult(0);
}
public Task SendSmsAsync(string number, string message)
{
// Plug in your SMS service here to send a text message.
return Task.FromResult(0);
}
}
public class MessageSenderOptions
{
public List<string> emails { get; set; }
public string emailServer { get; set; }
public string fromEmail { get; set; }
public string fromName { get; set; }
}
}
我在控制器中以这种方式调用它:
namespace WebApplication.Controllers
{
[Route("api/[controller]")]
public class MyController : Controller
{
private readonly Context _Context;
private IHostingEnvironment _environment;
private ILogger<MyController> _logger;
private MessageSender _messageSender;
public MyController(IOptions<RegistrarConnection> optionsAccessor,
Context dbContextd,
IHostingEnvironment environment,
ILogger<GestionAteliersController> logger,
MessageSender messageSender,
)
{
Options = optionsAccessor.Value;
_Context = dbContextd;
_environment = environment;
_logger = logger;
_messageSender = messageSender;
}
[Route("[action]")]
public IActionResult save([FromBody] Obj obj)
{
try
{
//code
string subjet = "Subject";
string message = "Message";
_messageSender.SendEmailAsync("test@gmail.ca", subjet, message);
var v = new { result = "OK" };
return this.Ok(v);
}
catch (Exception ex)
{
_logger.LogError(ex.ToString());
return new BadRequestObjectResult("Error");
}
}
这只是方法的一部分,并且一切正常。
我现在遇到的问题是我想调用此方法
_messageSender.SendEmailAsync("test@gmail.ca", subjet, message);
在控制器之外,在另一个类中,我不知道将它用作控制器很容易。
我不知道它如何使用类 MessageSender 的构造函数。
在 Startup.cs 文件中,方法 ConfigureServices 中有这部分代码:
services.Configure<MessageSenderOptions>(Configuration.GetSection("MessageSenderOptions"));
services.AddTransient<IEmailSender, MessageSender>();
services.AddTransient<ISmsSender, MessageSender>();
services.AddTransient<MessageSender, MessageSender>();
在我的文件 appsettings.json 中,有这部分代码:
"MessageSenderOptions": {
"emailServer": "smtp.gmail.ca",
"emails": [
"test@gmail.ca"
],
"fromEmail": "abb@gmail.ca",
"fromName": "Test"
}
所以我假设以某种方式调用mycontroller的构造函数时,就像这行完成所有工作一样:
_messageSender = messageSender;
通过调用 MessageSender 的构造方法,并使用 appsettings.json 文件中的参数 IOptions
但是我不知道如何在另一个类中使用它:
public class HelloJob : IJob
{
/*private readonly ILogger _logger;
*/
private MessageSender _messageSender;
_messageSender.SendEmailAsync("test@gmail.ca", subjet, message);
}
如何使用 Startup.cs 中使用的 appsettings.json 参数实例化 MessageSender ,以调用方法 SendEmailAsync
谢谢您的帮助
答案 0 :(得分:1)
检查此网址https://docs.microsoft.com/fr-fr/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.0
您需要在构造函数中传递类的实例
public class HelloJob : IJob
{
private readonly MessageSender _messageSender;
public HelloJob(MessageSender messageSender)
{
_messageSender = messageSender;
}
}