2

我在我的主項目中有FileHandler.ashx文件。castle windsor - 沒有爲此對象定義的無參數構造函數

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()); 

    } 
} 

但是,當我嘗試調用文件FileHandler.ashx我得到一個錯誤:

No parameterless constructor defined for this object.

的原因是什麼?如何解決它?

+0

您的網頁是否有依賴項解析程序?不知道它如何與ashx協同工作,但在mvc/web.api中,您必須設置一個新的依賴關係解析器來支持具有參數的ctors; http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver –

+0

你能提供更多的代碼/細節嗎?你如何將容器附加到你的項目?你有註冊FileHandler所依賴的服務嗎?你確定這是引入異常的類嗎? (我也不知道ashx是如何工作的) –

+0

這樣做:http://bugsquash.blogspot.com/2009/11/windsor-managed-httpmodules.html但是用於HttpHandlers而不是HttpModules。 –

回答

0

我認爲你必須提供一個空construtor像

public class FileHandler : IHttpHandler 
    { 
     private readonly IAccountService _accountService; 
     private readonly IAttachmentService _attachmentService; 

     public FileHandler() 
     { 
     } 
     public FileHandler(IAccountService accountService, IAttachmentService attachmentService) 
     { 
      _accountService = accountService; 
      _attachmentService = attachmentService; 
     } 

     .... 
    } 
0

這可能是溫莎城堡無法解析當前的構造函數需要IAccountServiceIAttachmentService的依賴關係。

在這種情況下,它可能會尋找一個無參數的使用。

確保上述依賴關係已註冊,並且windsor可以解決它們。

0

在你的web.config你有這樣的:

<castle> 
    <installers> 
     <install type="Your.Namespace.HandlersInstaller, Your.Namespace" /> 
    </installers> 
    </castle> 
0

溫莎城堡不知道如何創建IHttpHandler實例。處理程序沒有像ControlerFactory這樣的提示,所以你不能攔截處理程序創建過程。您有兩種選擇:

  1. 實現您的處理程序作爲控制器操作,並使用標準WindsorControlerFactory注入您的依賴關係。
  2. 提供參數的構造函數與溫莎作爲服務定位:

    ​​

this answer找出如何實現IContainerAccessor

相關問題