ASP.NET 与 Unity 2.0

发布于 2024-10-20 08:11:20 字数 966 浏览 3 评论 0 原文

谁能帮助我找到一些在不涉及 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???

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

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

发布评论

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

评论(1

乄_柒ぐ汐 2024-10-27 08:11:20

这取决于您的期望。大多数针对 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 stantandard PageHandlerFactory and builds page instance (resolve property dependencies).

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