属性的自动连接对我不起作用

发布于 2024-08-27 12:35:04 字数 1486 浏览 14 评论 0原文

在我的 Asp.Net 项目中,我想使用属性自动装配,例如我的 ILogger。基本上我将它作为属性放入我需要使用它的类中。就像下面这样。

public class UserControlBase : UserControl
{
    public ILogger CLogger { get; set; }
    ....
}


public partial class IPTracking : UserControlBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            //it works
            ILogger logger = ObjectFactory.GetInstance<ILogger>();
            logger.LogInfo(string.Format("Client IP: {0}", ip));
            //it does not work
            CLogger.LogInfo(string.Format("Client IP: {0}", ip));
        }
    }
}

但是,当调用继承的控件时,logger 为 null。我检查了容器,它的设置肯定如上面的实现所示。下面是从 Global.asax 调用的设置。

    public static void SetupForIoC()
    {
        Debug.WriteLine("SetupForIoC");
        ObjectFactory.Initialize(x =>
        {
            x.FillAllPropertiesOfType<ILogger>().Use<EntLibLogger>();
        });

        Debug.WriteLine(ObjectFactory.WhatDoIHave());
    }

感谢您的任何建议,提示? X.

更新
- 我之前没有提到,但它是 Asp.Net webforms 3.5。
- 我看不到我错过了什么。我想这可能是因为注入稍后涉及到过程并且没有在请求的类中设置。

链接到描述。用法: http://structuralmap.github.com/structuralmap/ConstructorAndSetterInjection.htm#section7

In my Asp.Net project I wanna use Property Auto-wiring, e.g. for my ILogger. Basically I placed it as Property into class where I need to use it. Like below.

public class UserControlBase : UserControl
{
    public ILogger CLogger { get; set; }
    ....
}


public partial class IPTracking : UserControlBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            //it works
            ILogger logger = ObjectFactory.GetInstance<ILogger>();
            logger.LogInfo(string.Format("Client IP: {0}", ip));
            //it does not work
            CLogger.LogInfo(string.Format("Client IP: {0}", ip));
        }
    }
}

However when calling in inherited control, logger is null. I checked the container and it'a definitely set as above implementation shows. Below is setting which is called from Global.asax.

    public static void SetupForIoC()
    {
        Debug.WriteLine("SetupForIoC");
        ObjectFactory.Initialize(x =>
        {
            x.FillAllPropertiesOfType<ILogger>().Use<EntLibLogger>();
        });

        Debug.WriteLine(ObjectFactory.WhatDoIHave());
    }

Thanks for any advice, tip? X.

Update:
- I didnt mentioned before, but its Asp.Net webforms 3.5.
- I can't see what I am missing. I guess it could be because the injection gets involved later in process and didnt get set in requested class.

Link to desc. of usage: http://structuremap.github.com/structuremap/ConstructorAndSetterInjection.htm#section7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏蝶舞 2024-09-03 12:35:04

尝试一下这样的事情。

FillAllPropertiesOfType<ILogger>().AlwaysUnique().Use(s => s.ParentType == null ? new Log4NetLogger(s.BuildStack.Current.ConcreteType) : new Log4NetLogger((s.ParentType)));

查看我的另一个 StackOverflow 答案,其中讨论了使用 StructureMap 自动连线记录器

Give something like this a shot.

FillAllPropertiesOfType<ILogger>().AlwaysUnique().Use(s => s.ParentType == null ? new Log4NetLogger(s.BuildStack.Current.ConcreteType) : new Log4NetLogger((s.ParentType)));

Check out another StackOverflow answer I have which discusses using StructureMap to auto wire loggers.

池予 2024-09-03 12:35:04

您实际上在用户控件上的哪里设置 CLogger 属性?另外,如果您想在页面中使用一个记录器,您可以让 User cotnrol 执行以下操作:

public ILogger CLogger
{
  get
  {
    if (this.Page is PageBase)
       return ((PageBase)this.Page).Logger;
    else
       return null;
  }
}

Where do you actually set the CLogger property on the user control? Also, if you wanted to use one logger within the page, you could have the User cotnrol do:

public ILogger CLogger
{
  get
  {
    if (this.Page is PageBase)
       return ((PageBase)this.Page).Logger;
    else
       return null;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文