在 HttpHandler 中使用 IoC 进行构造器/Setter 注入,可能吗?

发布于 2024-09-15 13:00:48 字数 529 浏览 6 评论 0 原文

我遇到了一个相当棘手的问题。可能有一个简单的解决方案,但我找不到它!

我有一个自定义 HttpHandler,我想处理请求,记录某些信息,然后在数据库中输入详细信息。我正在使用 NUnit 和温莎城堡。

所以我有两个接口;一个用于记录另一个用于数据输入,它们是构造函数注入的。我很快发现无法调用构造函数,因为总是调用默认的无参数构造函数。

所以我想我会使用Setter注入并让Castle Windsor来解决它。这实际上是有效的,当我使用 container.Resolve(); 时,我可以检查记录器是否不为空。 (在 Global.asax.cs 中的 Application_Start 中)

问题是虽然 Castle Windsor 可以创建实例,但 http 应用程序没有使用它???我认为??

基本上这样做的全部原因是能够通过模拟和单元测试单独测试记录器和数据存储库代码。

我有什么想法可以解决这个问题吗?

谢谢!

I've ran into a rather hairy problem. There is probably a simple solution to this but I can't find it!

I have a custom HttpHandler that I want to process a request, log certain info then enter the details in the database. I'm using NUnit and Castle Windsor.

So I have two interfaces; one for logging the other for data entry, which are constructor injected. I quickly found out that there is no way to call the constructor as the default parameterless constructor is always called instead.

So I thought I would use Setter injection and let Castle windsor sort it out. This actually works as when I use container.Resolve<CustomHttpHandler>(); I can check that the logger is not null. (In Application_Start in Global.asax.cs)

The problem is although Castle Windsor can create the instance the http application is not using it??? I think??

Basically the whole reason for doing it this way was to be able to test the logger and data repository code in isolation via mocking and unit testing.

Any ideas how I can go about solving this problem?

Thanks!

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

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

发布评论

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

评论(2

不醒的梦 2024-09-22 13:00:48

不可能,至少不可能直接。 IHttpHandler 对象由 ASP.NET 运行时实例化,并且不允许 Windsor 参与其创建。您可以:

  • 通过使用容器作为服务定位器来拉取依赖项。
  • 设置一个基本处理程序,用于创建、注入和委托给您自己的处理程序(请参阅如何 Spring 做到了
  • 使用容器作为处理整个请求的另一个服务的服务定位器(如 saret 解释

Not possible, at least not directly. IHttpHandler objects are instantiated by the ASP.NET runtime and it doesn't allow Windsor to get involved in its creation. You can either:

  • Pull dependencies, by using the container as a service locator.
  • Set up a base handler that creates, injects and delegates to your own handlers (see how Spring does it)
  • Use the container as a service locator for another service that handles the entire request (as saret explained)
红玫瑰 2024-09-22 13:00:48

您可以做的就是让 HttpHandler 调用另一个实际处理请求的对象。所以在你的 HttpHandler 的 ProcessRequest 方法中你会做这样的事情:

public void ProcessRequest(HttpContext context)
{
 var myHandlerObject = container.Resolve<HandlerObject>();
 myHandlerObject.ProcessRequest(context or some state/info that is required)
}

What you could do is have the HttpHandler call out to another object that actually handles the request. so in your HttpHandler's ProcessRequest method you would do something like this:

public void ProcessRequest(HttpContext context)
{
 var myHandlerObject = container.Resolve<HandlerObject>();
 myHandlerObject.ProcessRequest(context or some state/info that is required)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文