我遇到了一个相当棘手的问题。可能有一个简单的解决方案,但我找不到它!
我有一个自定义 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!
发布评论
评论(2)
不可能,至少不可能直接。 IHttpHandler 对象由 ASP.NET 运行时实例化,并且不允许 Windsor 参与其创建。您可以:
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:
您可以做的就是让 HttpHandler 调用另一个实际处理请求的对象。所以在你的 HttpHandler 的 ProcessRequest 方法中你会做这样的事情:
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: