A clean way to specify parameters for a service when performing DI

So, I have a service, for example, let's say this is an email service in ASPNET Core.

When I add my service to the ASPNET DI container, I would like to apply the following template on my IServiceCollection to configure my service.

public interface IEmailService { void SendMail(string recipient, string message); } public void ConfigureServices(IServiceCollection services) { //configures my service services.AddEmailService<MyEmailService>(options => options.UseEmailServer(sender, smtpHost, smtpPort, smtpPassword)); } 

I would like to know what is the best way to do this, if possible. I am sure that I will need to make an extension method for the .AddEmailService () method in IServiceCollection, but for something else, I am not sure where to start or look.

+6
source share
1 answer

Here's an example application with comments so you know what different things do:

 public class Startup { public void ConfigureServices(IServiceCollection services) { // Add the options stuff. This will allow you to inject IOptions<T>. services.AddOptions(); // This will take care of adding and configuring the email service. services.AddEmailService<MyEmailService>(options => { options.Host = "some-host.com"; options.Port = 25; options.Sender = " firstname@lastname.com "; options.Username = "email"; options.Password = "sup4r-secr3t!"; }); } public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) { // Make sure we add the console logger. loggerFactory.AddConsole(); app.Use(async (context, next) => { // Retrieve the email service from the services. var emailService = context.RequestServices.GetRequiredService<IEmailService>(); // Send the email await emailService.SendMail(" hello@recipient.com ", "Hello World!"); }); } public static void Main(string[] args) { WebApplication.Run<Startup>(args); } } public interface IEmailService { Task SendMail(string recipient, string message); } public class EmailOptions { public string Sender { get; set; } public string Host { get; set; } public int Port { get; set; } public string Username { get; set; } public string Password { get; set; } } public class MyEmailService : IEmailService { public MyEmailService(IOptions<EmailOptions> options, ILogger<MyEmailService> logger) { Options = options; // This contains the instance we configured. Logger = logger; } private IOptions<EmailOptions> Options { get; } private ILogger<MyEmailService> Logger { get; } public Task SendMail(string recipient, string message) { // Send the email var builder = new StringBuilder(); builder.AppendLine($"Host: {Options.Value.Host}"); builder.AppendLine($"Port: {Options.Value.Port}"); builder.AppendLine($"Username: {Options.Value.Username}"); builder.AppendLine($"Password: {Options.Value.Password}"); builder.AppendLine("---------------------"); builder.AppendLine($"From: {Options.Value.Sender}"); builder.AppendLine($"To: {recipient}"); builder.AppendLine("---------------------"); builder.AppendLine($"Message: {message}"); Logger.LogInformation(builder.ToString()); return Task.FromResult(0); } } public static class ServiceCollectionExtensions { public static IServiceCollection AddEmailService<TEmailService>(this IServiceCollection services, Action<EmailOptions> configure) where TEmailService : class, IEmailService { // Configure the EmailOptions and register it in the service collection, as IOptions<EmailOptions>. services.Configure(configure); // Add the service itself to the collection. return services.AddSingleton<IEmailService, TEmailService>(); } } 

And here is the application running on the console:

Application launch

As you can see, the application pulls some information from the configured EmailOptions , and some data form the arguments passed to.

EDIT : these are the necessary packages:

 "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final", "Microsoft.Extensions.OptionsModel": "1.0.0-rc1-final", "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final" 
+8
source

All Articles