2017-08-01 31 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)

相关问题