Roman Sterlin
Roman Sterlin

Reputation: 1617

Injecting a class with IOptions in the constructor with Autofac

I am using Autofac as a container that I am using in another console app that uses the same objects as my main web app, and am trying to inject a class that has IOptions inside his constructor, here's how it looks like:

 public class Mailer : IMailer
{
    private readonly StmpSettings _stmpSettings;
    public Mailer(IOptions<StmpSettings> stmpSettings)
    {
        _stmpSettings = stmpSettings.Value;
    }
    public async Task SendEmailAsync(string email, string subject, string body)
    {
        try
        {
            var message = new MimeMessage();
            message.From.Add(new MailboxAddress(_stmpSettings.SenderName, _stmpSettings.SenderEmail));
            message.To.Add(new MailboxAddress(email));
            message.Subject = subject;
            message.Body = new TextPart("html")
            {
                Text = body
            };
            using (var client = new SmtpClient())
            {
                client.ServerCertificateValidationCallback = (s, c, h, e) => true;
                //I am using my own certificate for https. so when trying to connect to google smtp it cannot authenticate the certificate. I used this temp workaround: SecureSocketOptions.Auto.
                //It means that the mail wont be encrypted.
                //https://github.com/jstedfast/MailKit/blob/master/FAQ.md#SslHandshakeExceptionhttps://support.google.com/mail/?p=InvalidSecondFactor look for more secure solutions.

                await client.ConnectAsync(_stmpSettings.Server, _stmpSettings.Port, SecureSocketOptions.Auto);
                await client.AuthenticateAsync(_stmpSettings.Username, _stmpSettings.Password);
                await client.SendAsync(message);
                await client.DisconnectAsync(true);
            }
        }
        catch (Exception e)
        {
            throw e;
        }
    }
}

as you can see the constructor is getting some settings from the injected stmpSettings, any idea to how I could inject it with the autofac container?

so far I have

builder.RegisterType<Mailer>().As<IMailer>();

but I need to include the things that the constructor injects as well. having a real hard time understanding to how to do it, any help would be appreciated!

Upvotes: 1

Views: 722

Answers (1)

Farhad Zamani
Farhad Zamani

Reputation: 5861

With Populate method you can register services to Autofac that registered by .Net Core services.

Create a method like this

public static IServiceProvider BuildAutofacServiceProvider(this IServiceCollection services)
{
    var containerBuilder = new ContainerBuilder();
    containerBuilder.Populate(services);
    var container = containerBuilder.Build();
    return new AutofacServiceProvider(container);
}

Change the return type of ConfigureServices method to IServiceProvider

public IServiceProvider ConfigureServices(IServiceCollection services)
{
      //register services
    return services.BuildAutofacServiceProvider();
}

Upvotes: 1

Related Questions