问题描述
该问题与我的其他帖子.
好吧,经过一番混乱之后,我决定采用这种方式.尽管我在NUnit中遇到以下错误,但运行它时似乎可以正常工作:无法加载文件或程序集"Castle.Core,Version = 1.0.3.0,Culture = neutral,PublicKeyToken = 407dd0808d44fbdc"或其中之一依赖关系.找到的程序集的清单定义与程序集引用不匹配. (来自HRESULT的异常:0x80131040)因此,不确定在那里发生了什么??
Ok so after a bit more messing around I decided to do it this way. Which seems to work fine when I run it, although I'm getting the following error in NUnit: Could not load file or assembly 'Castle.Core, Version=1.0.3.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) So not sure what is happening there???
只想知道其他人对设计的看法,以及是否有明显的不行"或改进. IE.基本处理程序的构造函数是实例化windsor组件的好地方,还是有更好的地方呢?就像我在原始帖子中所说的那样,这样做的想法是使组件保持良好的解耦并使单元测试变得容易.我还应该添加我是单元测试,模拟的新手.谢谢!
Just wanted to know what others thought about the design and if there are any obvious 'no no's' or improvements. I.e. Is the constructor of the base handler a good place to instantiate the windsor component or is there a better place to do this? As I said in the original post the idea behind doing things this way was to keep the components nicely decoupled and to make unit testing easy. I should also add I'm new to unit testing, mocking. Thanks!
public abstract class BaseHttpHandler : IHttpHandler
{
private HttpContext _httpContext;
private ILogger _logger;
private IDataRepository _dataRepository;
protected HttpRequest Request { get { return _httpContext.Request; } }
protected HttpResponse Response { get { return _httpContext.Response; } }
protected bool IsRequestFromUAD { get { return Request.UserAgent == null ? false : Request.UserAgent.Equals("UAD"); } }
protected ILogger Logger { get { return _logger; } }
protected IDataRepository DataRepository { get { return _dataRepository; } }
public virtual bool IsReusable { get { return false; } }
public BaseHttpHandler()
{
var container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));
_logger = container.Resolve<ILogger>();
_dataRepository = container.Resolve<IDataRepository>();
}
public void ProcessRequest(HttpContext context)
{
_httpContext = context;
ProcessRequest(new HttpContextWrapper(context));
}
public abstract void ProcessRequest(HttpContextBase context);
}
public class UADRecordHttpHandler : BaseHttpHandler
{
public override void ProcessRequest(HttpContextBase context)
{
if (IsRequestFromUAD)
{
using (var reader = new StreamReader(context.Request.InputStream))
{
string data = reader.ReadToEnd();
if (Logger != null)
Logger.Log(data);
if(DataRepository != null)
DataRepository.Write(data);
context.Response.Write(data);
}
}
else
ReturnResponse(HttpStatusCode.BadRequest);
}
}
推荐答案
这很不好,您在这里正在做什么.每个应用程序应具有一个容器实例,而使用此代码,每个请求将具有一个实例.
That's a very bad thing to do, what you're doing here. You should have one instance of the container per application, while with this code you will have one per each request.
这篇关于使用IoC容器作为HttpHandler的服务定位器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!