Unity未注入基类中定义的依赖项

发布于 2024-10-22 18:42:40 字数 455 浏览 4 评论 0原文

我正在将 Unity 2.0 集成到我的 ASP.NET 应用程序中(使用 UnityPageHandlerFactory 方法),并且一切正常,直到我尝试将依赖项之一移至 PageBase 类中,然后由所有页面共享该类。调用 BuildUp 时永远不会设置此属性。

我正在使用描述的 UnityPageHandlerFactory 类 此处,它使用 BuildUp(type, object) 方法在请求时将依赖项注入到每个页面中。只要我在声明的类型中定义了属性,这些属性就会被注入。但在基类中定义的属性永远不会被设置。

我还需要做些什么吗?在我看来,这应该是自动的。

I am integrating Unity 2.0 into my ASP.NET application (using the UnityPageHandlerFactory approach) and have everything working great until I tried to move one of the dependencies into a PageBase class that would then be shared by all pages. This property is never set when BuildUp is called.

I'm using the UnityPageHandlerFactory class described here which uses the BuildUp(type, object) method to inject dependencies into each page when it is requested. As long as I have the properties defined in the declared type, the properties are injected. But properties defined in a base class are never set.

Is there something else I need to do? It seems to me that this should be automatic.

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

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

发布评论

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

评论(2

万劫不复 2024-10-29 18:42:40

事实证明,我使用的是 BuildUp 方法的不同重载,并且使用引用示例中的方法解决了我的问题。

我正在使用 BuildUp(object) 但它不起作用。当我切换到 BuildUp(Type, object) 时,一切都像魅力一样!

我不确定为什么,但只能假设它与第一次重载中解析类型的方式有关,而不是与显式提供类型时发生的情况有关。

不管怎样,这个小小的改变解决了我所有的问题。

It turns out that I was using a different overload of the BuildUp method and going with the one in the cited example fixed my problem.

I was using BuildUp(object) and it was not working. When I switched to BuildUp(Type, object), everything works like a charm!

I'm not sure why but can only assume that it has something to do with the way the type is resolved in the first overload as opposed to what happens when the type is explicitly provided.

Either way, making this little change fixed all of my problems.

心房的律动 2024-10-29 18:42:40

你能展示你的代码的相关部分吗?这就是我所拥有的,这似乎有效:

    class InjectedClass
    {

    }

    class  MyBase
    {
            [Dependency]
            public InjectedClass Dependency { get; set; }
    }

    class MyClass : MyBase
    {

    }

    class Program
    {
            static void Main(string[] args)
            {
                    UnityContainer uc = new UnityContainer();
                    uc.RegisterType<InjectedClass>();

                    MyClass m = new MyClass();
                    uc.BuildUp(m);

            }
    }

我还在 asp.net 应用程序中使用 UnityPageHandlerFactory 对此进行了测试,并且类似地可以看到 InjectedClass 被注入到我的页面中,尽管依赖属性位于基类上。

Can you show the relevant parts of you code? This is what I have and this seems to work:

    class InjectedClass
    {

    }

    class  MyBase
    {
            [Dependency]
            public InjectedClass Dependency { get; set; }
    }

    class MyClass : MyBase
    {

    }

    class Program
    {
            static void Main(string[] args)
            {
                    UnityContainer uc = new UnityContainer();
                    uc.RegisterType<InjectedClass>();

                    MyClass m = new MyClass();
                    uc.BuildUp(m);

            }
    }

I also tested this with UnityPageHandlerFactory in asp.net application and similarly can see that InjectedClass is injected into my page, although the dependency property is on the base class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文