Search code examples
c#asp.netwebformsautofac

how to resolve multiple dependencies of the same type in a System.Web.UI.Page where these dependencies implement a generic interface


In the BasePage.cs class I have the following IIndex property which should have two "or more" implementations of the INotificationService that can be accessed using a key.

public class BasePage : Page
{
    public IIndex<string, INotificationService<INotification>> NotificationServices { get; set; }

    public INotificationService<INotification> EmailService
    {
        get
        {
            return NotificationServices["emailService"];
        }
    }
    public INotificationService<INotification> FaxService
    {
        get
        {
            return NotificationServices["faxService"];
        }
    }
}

Concrete Classes:

public class FaxNotificationService : INotificationService<FaxNotification>
{
    private IClient _smtpClient;
    public FaxNotificationService(IClient smtpClient)
    {
        _smtpClient = smtpClient;
    }
    public void Send(FaxNotification notification)
    {
        _smtpClient.Send(notification);
    }
}

public class EmailNotificationService : INotificationService<EmailNotification>
{
    private IClient _smtpClient;
    public EmailNotificationService(IClient smtpClient)
    {
        _smtpClient = smtpClient;
    }
    public void Send(EmailNotification notification)
    {
        _smtpClient.Send(notification);
    }
}

in Global.asax.cs

void Application_Start(object sender, EventArgs e)
    {
        var emailSmtp = new SmtpWrapper(new SmtpClient
        {
            ...
        });


        var faxSmtp = new SmtpWrapper(new SmtpClient
        {
           ...
        });

        var builder = new ContainerBuilder();

// before having the generic interface the following commented code worked perfectly fine

        //builder.RegisterType<EmailNotificationService>()
        //        .Named<INotificationService>("emailService")
        //        .WithParameter("smtpClient", emailSmtp);

        //builder.RegisterType<FaxNotificationService>()
        //       .Named<INotificationService>("faxService")
        //       .WithParameter("smtpClient", faxSmtp);

        builder.RegisterType<EmailNotificationService>()
            .Named<INotificationService<INotification>>("emailService")
            .WithParameter("smtpClient", emailSmtp);


        builder.RegisterType<BasePage>().AsSelf();

        var build = builder.Build();
        _containerProvider = new ContainerProvider(build);

        using (var scope = build.BeginLifetimeScope())
        {
            var service = scope.Resolve<BasePage>();
        }
    }

In Global.asax.cs I tried to do somehow the same to register the EmailNotificationService but I'm getting an exception:

The type 'NotificationServices.EmailNotificationService' is not assignable to service 'emailService (Shared.NotificationServices.Abstractions.INotificationService`1[[Shared.NotificationServices.Abstractions.INotification ...]

Now I know why it's not working. because in C# it's not even possible to do the following:

INotificationService<INotification> service = new EmailNotificationService(new SmtpWrapper(new SmtpClient())); 

the later line of code will result a compile time error:

Cannot implicitly convert type 'NotificationServices.EmailNotificationService' to 'Shared.NotificationServices.Abstractions.INotificationService'. An explicit conversion exists (are you missing a cast?)

So any ideas people : )


Solution

  • They may implement the same interface but they can't necessarily be treated the same. Type is an important differentiator. Solving this is an FAQ in the docs.