2017-08-01 59 views
2

我使用Microsoft擴展的依賴注入在Sitecore的8.2更新4螺旋框架,下面是我的代碼:如何在Http Handler Sitecore中使用Microsoft.Extensions.DependencyInjection?

public class TestTextHandler : IHttpHandler 
{ 
    private readonly ITest _test; 

    public TestTextHandler(Test test) 
    { 
     _test = test; 
    } 
} 

public interface ITest 
{ 
} 

public class Test : ITest 
{ 
} 

public class RegisterContainer : IServicesConfigurator 
{ 
    public void Configure(IServiceCollection serviceCollection) 
    { 
     serviceCollection.AddTransient<ITest, Test>(); 
    } 
} 

一個補丁文件:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> 
    <sitecore> 
    <services> 
     <configurator type="XX.XX.RegisterContainer, XX.XX" /> 
    </services> 
    </sitecore> 
</configuration> 

我對類型「XX得到錯誤構造.XX.TestTextHandler「未找到。

回答

0

我剛剛試過你的代碼(加上IHttpHandler的接口實現),它使用Habitat解決方案時工作得很好。檢查以確保程序集發佈在/ bin文件夾中並且配置正確。 (FY:我甚至檢查ShowServicesConfig.aspx,以確保它得到加載,它短暫的一生一樣。)

+0

謝謝你,讓我看看 – SKG

+0

不是爲我工作。如果你能分享你的代碼,那將會很棒。 – SKG

1

你所得到的錯誤,因爲你已經配置了TestTextHandler採取具體Test對象,而不是一個繼承您的ITest接口的對象。

您需要將構造函數聲明更改爲: public TestTextHandler(ITest test)

相關問題