【问题标题】:how to resolve multiple dependencies of the same type in a System.Web.UI.Page where these dependencies implement a generic interface如何在 System.Web.UI.Page 中解决相同类型的多个依赖项,其中这些依赖项实现通用接口
【发布时间】:2019-06-13 15:32:29
【问题描述】:

在 BasePage.cs 类中,我有以下 IIndex 属性,它应该有两个“或更多”的 INotificationService 实现,可以使用键访问。

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"];
        }
    }
}

具体类:

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);
    }
}

在 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>();
        }
    }

在 Global.asax.cs 中,我尝试以某种方式注册 EmailNotificationService,但出现异常:

'NotificationServices.EmailNotificationService' 类型不可分配给服务'emailService (Shared.NotificationServices.Abstractions.INotificationService`1[[Shared.NotificationServices.Abstractions.INotification ...]

现在我知道为什么它不起作用了。 因为在 C# 中甚至不可能做到以下几点:

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

后面的代码行会导致编译时错误:

无法将类型“NotificationServices.EmailNotificationService”隐式转换为“Shared.NotificationServices.Abstractions.INotificationService”。存在显式转换(您是否缺少演员表?)

所以任何想法的人:)

【问题讨论】:

    标签: c# asp.net webforms autofac


    【解决方案1】:

    它们可能实现相同的接口,但不一定会被同等对待。类型是一个重要的区别。 Solving this is an FAQ in the docs.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-18
      • 2011-05-06
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 2022-12-15
      • 1970-01-01
      • 2016-06-19
      相关资源
      最近更新 更多