解决Autofac“属性注入” /ASP.NET Webforms应用程序中相同类型的2个属性

时间:2019-06-06 09:49:43

标签: c# asp.net webforms autofac

我有一个BasePage类,该类具有两个具有相同依赖类型的属性,并且找不到使用Autofac提供所需参数的方法

我尝试注册类型,并且两个BasePage属性现在都指向最后一个注册的组件,即FaxSender,我在TryGetDeclaringProperty函数中添加了一个断点,它可以正常工作并检查BasePage类的属性名称。 / p>

[更新示例]

public class BasePage : System.Web.UI.Page
{
    public ISender EmailSender { get; set; }
    public ISender FaxSender { get; set; }
}

public class EmailSender : ISender
{
    private readonly SmtpClient _smtpClient;

    public EmailSender(SmtpClient smtpClient)
    {
        _smtpClient = smtpClient;
    }
    public void Send(INotification notification)
    {
        //...
    }
}

public class FaxSender : ISender
{
    private readonly SmtpClient _smtpClient;

    public FaxSender(SmtpClient smtpClient)
    {
        _smtpClient = smtpClient;
    }
    public void Send(INotification notification)
    {
        //...
    }
}

在Global.asax.cs

  var emailSmtp = new SmtpClient
        {
            ...
        };

        var emailSender = new EmailSender(emailSmtp);

        var faxSmtp = new SmtpClient
        {
            ...
        };

        var faxSender = new FaxSender(faxSmtp);

        var builder = new ContainerBuilder();

//--------------------------------
        builder.RegisterType<BasePage>()
            .WithProperties(new Parameter[]{
                new NamedPropertyParameter("EmailSender", emailSender),
                new NamedPropertyParameter("FaxSender", faxSender),
            });
 //--------------------------------  

    //OR

//--------------------------------

builder.RegisterType<EmailSender>()
            .Named<ISender>("email")
            .WithParameter("smtpClient", emailSmtp);
        builder.RegisterType<FaxSender>()
            .Named<ISender>("fax")
            .WithParameter("smtpClient", faxSmtp);

        builder.RegisterType<BasePage>()
            .AsSelf()
            .WithProperties(new Parameter[] {
                new ResolvedParameter(
                    (pi, c) => {
                        PropertyInfo ppi = null;
                        if (pi.TryGetDeclaringProperty(out ppi)) {
                            return ppi.Name == "EmailSender";
                        } else {
                            return false;
                        }
                    },
                    (pi, c) => c.ResolveNamed<ISender>("email")),
                new ResolvedParameter(
                    (pi, c) => {
                        PropertyInfo ppi = null;
                        if (pi.TryGetDeclaringProperty(out ppi)) {
                            return ppi.Name == "FaxSender";
                        } else {
                            return false;
                        }
                    },
                    (pi, c) => c.ResolveNamed<ISender>("fax"))
            });

//--------------------------------
//and then

        var container = builder.Build();

        _containerProvider = new ContainerProvider(container);

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

在Default.aspx.cs中

protected void Page_Load(object sender, EventArgs e)
{
    //Object reference not set to an instance of an object.  [Exception in both cases]
    EmailSender.Send(new EmailNotification(...);

    FaxSender.Send(new FaxNotification(...));
}

1 个答案:

答案 0 :(得分:0)

由于ASP.net Web表单的体系结构,Page实例不是由 Autofac 创建的,因此无法为页面实例配置任何注册。在这种情况下,将不会使用WithProperty方法。

在您的情况下,一种解决方案是手动解决依赖性。

在您的BasePage组件中

public ISender EmailSender
{
    get
    {
        var cpa = (IContainerProviderAccessor)this.Context.ApplicationInstance;
        return cpa.ContainerProvider.RequestLifetime.ResolveNamed<ISender>("email");
    }
}

另一种解决方案是使用IIndex<TKey, TValue>。除了拥有2个属性,您只能拥有一个包含所有ISender的属性。

public IIndex<String, ISender> Senders {get; set; } 

当您需要特定的发件人时,可以通过

进行访问。
ISender emailSender = this.Senders["email"]; 

顺便说一句,如果您想要预期的行为,但对于由 Autofac 创建的实例,则应使用NamedPropertyParameter而不是NamedParameter

// not working for webform !!! 
builder.RegisterType<BasePage>()
       .WithProperties(new Parameter[]{
           new NamedPropertyParameter("X", emailSender),
           new NamedPropertyParameter("Y", faxSender),
       });

如果要通过依赖项注入获取值,则没有简便的方法。

Autofac内部依赖于此扩展方法:

public static class ParameterInfoExtensions
{
  public static bool TryGetDeclaringProperty(this ParameterInfo pi, out PropertyInfo prop)
  {
    MethodInfo mi = pi.Member as MethodInfo;
    if (mi != (MethodInfo)null && mi.IsSpecialName 
        && mi.Name.StartsWith("set_", StringComparison.Ordinal) 
        && mi.DeclaringType != (Type)null)
    {
      prop = mi.DeclaringType.GetTypeInfo().GetDeclaredProperty(mi.Name.Substring(4));
      return true;
    }
    prop = null;
    return false;
  }
}

,您可以通过以下方式将其与ResolvedParameter配合使用:

builder.RegisterType<EmailSender>() 
       .Named<ISender>("email")
       .WithParameter("smtpClient", emailSmtp);
builder.RegisterType<FaxSender>()
       .Named<ISender>("fax")
       .WithParameter("smtpClient", faxSmtp);

builder.RegisterType<BasePage>()
       .AsSelf()
       .WithProperties(new Parameter[] {
           new ResolvedParameter(
             (pi, c) => {
               PropertyInfo ppi = null;
               if (pi.TryGetDeclaringProperty(out ppi)) {
                 return ppi.Name == "SmtpClient";
               } else {
                 return false;
               }
             },
             (pi, c) => c.ResolveNamed<ISender>("email")),
           new ResolvedParameter(
             (pi, c) => {
               PropertyInfo ppi = null;
               if (pi.TryGetDeclaringProperty(out ppi)) {
                 return ppi.Name == "FaxClient";
               } else {
                 return false;
               }
           },
           (pi, c) => c.ResolveNamed<ISender>("fax"))
       });