谁能帮助我找到一些在不涉及 ASP.NET MVC 的 ASP.NET 中使用 Unity 2.0 的好资源?!?
我们没有使用 MVC,并且我正在努力让 Unity 按照我读过的几个示例将依赖项注入到我的页面中(这些示例均基于 David Hayden 的工作,因此它们都提供相同的示例和代码)。
更新
我尝试走 PageHandlerFactory 路线,但示例 (此处)不完整,并且本文没有提供源代码。
因此,我决定尝试 此处和此处。同样,除了显示的内容之外,没有任何可用的源代码,因此很难解决问题。
我现在遇到的问题是,所有管道似乎都正确连接,但 Buildup 方法对我的页面没有任何作用。当我在模块代码中设置断点并且代码按预期执行时,我可以看到所有类型都已注册在容器中。但 Page_Load 事件处理程序中的断点显示依赖项全部为空。
所讨论的属性是公共的,具有 setter 和 getter,并用 Dependency 属性进行标记。我尝试过使用和不使用该属性,使用和不使用映射名称,......我能想到的每一种组合都不起作用。
我错过了什么???
Can anyone help point me to some good resources for working with Unity 2.0 in an ASP.NET that doesn't talk about ASP.NET MVC?!?
We are not using MVC and I am struggling to get Unity to inject dependencies into my pages following the couple of examples I've read (which are all based on David Hayden's work so they are all presenting the same examples and code).
UPDATE
I tried to go the PageHandlerFactory route but the example (here) is incomplete and no source code is available to accompany the article.
So, I decided to try the custom HttpModule approach described here and here. Again, no source code is available beyond what is shown so it is difficult to troubleshoot issues.
The problem I have now is that all of the plumbing appears to be wiring up correctly but the Buildup method does nothing to my page. I can see all of the types are registered in the container when I set a break-point in the module code and the code is executing as expected. But a break-point in the Page_Load event handler shows that the dependencies are all null.
The property in question is public, with a setter and getter, and is marked with the Dependency attribute. I've tried with and without the attribute, with and without a mapping name, ... every combination I could think of and nothing works.
What am I missing???
发布评论
评论(1)
这取决于您的期望。大多数针对 MVC 的示例都提供自定义控制器工厂,它允许通过依赖项注入创建控制器。这对于 Web 表单来说确实也是可能的,但是您必须将依赖项注入到页面中,而不是控制器。为此,您必须将
PageHandlerFactory
替换为自定义实现。您可以创建自己的
PageHandlerFactory
实现,它将能够直接解析页面并注入构造函数中定义的依赖项,或者您可以使用其中之一 (此处,此处和此处),它使用标准PageHandlerFactory
并构建页面实例(解决属性依赖关系)。It depends what you expect. Most exemples targeting MVC present custom controller factory which allows creating controllers with dependency injection. This is indeed also possible with web forms but instead of controller you must inject dependencies into pages. To do this you must replace
PageHandlerFactory
with custom implementation.You can create your own implementation of
PageHandlerFactory
which will be able to resolve pages directly and inject dependencies defined in constructor or you can use one of these (here, here and here) which instead uses stantandardPageHandlerFactory
and builds page instance (resolve property dependencies).