Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# castle windsor-未为此对象定义无参数构造函数_C#_Asp.net_Dependency Injection_Inversion Of Control_Castle Windsor - Fatal编程技术网

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()的生活方式;
}
}
但是当我试图调用file
FileHandler.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。