C# castle windsor-未为此对象定义无参数构造函数
我的主项目中有C# castle windsor-未为此对象定义无参数构造函数,c#,asp.net,dependency-injection,inversion-of-control,castle-windsor,C#,Asp.net,Dependency Injection,Inversion Of Control,Castle Windsor,我的主项目中有FileHandler.ashx文件 public class FileHandler : IHttpHandler { private readonly IAccountService _accountService; private readonly IAttachmentService _attachmentService; public FileHandler(IAccountService accountService, IAttachment
FileHandler.ashx
文件
public class FileHandler : IHttpHandler
{
private readonly IAccountService _accountService;
private readonly IAttachmentService _attachmentService;
public FileHandler(IAccountService accountService, IAttachmentService attachmentService)
{
_accountService = accountService;
_attachmentService = attachmentService;
}
....
}
另外,我还有HandlerInstaller
:
public class HandlersInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Classes.FromThisAssembly()
.Where(Component.IsInSameNamespaceAs<FileHandler>())
.WithService.DefaultInterfaces()
.LifestyleSingleton());
}
}
公共类HandlersInstaller:IWindsorInstaller
{
public void安装(IWindsorContainer、IConfigurationStore)
{
container.Register(Classes.fromthissembly())
.Where(Component.IsInSameNamespaceAs())
.WithService.DefaultInterfaces()
.singleton()的生活方式;
}
}
但是当我试图调用fileFileHandler.ashx时,我得到一个错误:
没有为此对象定义无参数构造函数
原因是什么?如何修复它?我认为你必须提供一个这样的空构造函数
public class FileHandler : IHttpHandler
{
private readonly IAccountService _accountService;
private readonly IAttachmentService _attachmentService;
public FileHandler()
{
}
public FileHandler(IAccountService accountService, IAttachmentService attachmentService)
{
_accountService = accountService;
_attachmentService = attachmentService;
}
....
}
可能是Castle Windsor无法解析当前构造函数所需的依赖项IAccountService
和IAttachmentService
在这种情况下,它可能是寻找一个无参数的,而不是使用
确保已注册上述依赖项,windsor可以解决这些依赖项。在您的web.config中,是否有以下内容:
<castle>
<installers>
<install type="Your.Namespace.HandlersInstaller, Your.Namespace" />
</installers>
</castle>
温莎城堡不知道如何创建IHttpHandler
实例。处理程序没有类似于ControlerFactory
的东西,因此您无法拦截处理程序创建过程。您有两个选择:
将处理程序实现为控制器操作,并使用标准的windsorcontrollerfactory
注入依赖项
提供使用Windsor作为服务定位器的无参数构造函数:
public class FileHandler : IHttpHandler
{
readonly IAccountService accountService;
readonly IAttachmentService attachmentService;
public FileHandler()
{
var containerAccessor = HttpContext.Current.ApplicationInstance as IContainerAccessor;
var container = conatinerAccessor.Container;
accountService = container.Resolve<IAccountService>();
attachmentService = container.Resolve<IAttachmentService>();
}
public FileHandler(IAccountService accountService, IAttachmentService attachmentService)
{
this.accountService = accountService;
this.attachmentService = attachmentService;
}
...
}
公共类文件处理程序:IHttpHandler
{
只读IAccountService accountService;
只读IAttachmentService附件服务;
公共文件处理程序()
{
var containerAccessor=HttpContext.Current.ApplicationInstance作为IContainerAccessor;
var container=conatinerAccessor.container;
accountService=container.Resolve();
attachmentService=container.Resolve();
}
公共文件处理程序(IAccountService accountService、IAttachmentService attachmentService)
{
this.accountService=accountService;
this.attachmentService=attachmentService;
}
...
}
请参阅以了解如何实现IContainerAccessor
您的网页是否有依赖项解析程序?不知道它是如何与ashx一起工作的,但是在mvc/web.api中,您必须设置一个新的依赖项解析器来支持带有参数的CTOR;你能提供更多的代码/细节吗?如何将容器附加到项目中?您是否已注册FileHandler所依赖的服务?您确定是这个类引入了异常吗?(我也不知道ashx是如何工作的)这样做的:但是对于HttpHandler而不是HttpModule。