MissingMethodException:没有为类型定义无参数构造函数



我有一个使用.net core 5的Blazor应用程序。我正在尝试运行一个页面来触发电子邮件发送(EmailSample(,在我尝试添加一个用于记录错误的构造函数注入之前,该页面运行良好。一旦我添加构造函数注入并重新运行页面,我就会得到一个No parameterless constructor defined for type错误。我没有正确使用这个吗?如何修复,这样我就不会出现错误。如果我能想出如何在Startup.cs/ConfigureServices/services.AddSingleton< ??? >中添加记录器作为服务,我就可以绕过这个问题然后只在EmailSample类中执行[Inject],但不知道如何执行。

[Inject]
public ILogger _logger { get; set; }

导致错误的代码是…(注意:设置为分部类,而不是在.rarzor页面@code{}中(

public partial class EmailSample
{
    private readonly ILogger<EmailSample> _logger;
    public EmailSample (ILogger<EmailSample> logger)
    {
        _logger = logger;
    }
[Inject]
    public IMailService _mailService { get; set; }
    
    public void SendEmail()
    {
        string emailTo = "test@test.com";
        string emailFrom = "noreply@test.com";
        string emailSubject = "This is a test email.";
        string emailType = "html";         
        string emailCc = string.Empty;
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.AppendLine("Hello" + " " + emailTo);
        sb.AppendLine("</br>");
        sb.AppendLine("This is a test of the email functionality");
        sb.AppendLine("</br>");
        sb.AppendLine("Best Regards,");
        sb.AppendLine("</br>");
        sb.AppendLine("Signature");
        string emailBody = sb.ToString();
        try
        {
            throw new Exception("This is an email sample exception test");
            //Task tSendEmail = _mailService.SendEmailAsync(emailTo, emailSubject, emailBody, emailFrom, emailCc, emailType);
            //if (tSendEmail.IsCompletedSuccessfully)
            //{
            //    _statusMessage = "The email has been sent successfully.";
            //}
            //else
            //{
            //    _statusMessage = "Failed to send email successfully.";
            //}
         
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "The email for { emailto } failed to send.", emailTo);
        }
    }
    public void OnGet()
    {
        _statusMessage = "";
    }
}
[Inject]
private ILogger<EmailSample> logger {get; set;}

不要忘记名称空间Microsoft.Extensions.Logging

相关内容

  • 没有找到相关文章

最新更新