2

我在带有 Helix 框架的 Sitecore 8.2 更新 4 中使用 Microsoft 扩展依赖注入,下面是我的代码:

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”上收到错误构造函数。

4

2 回答 2

1

您收到错误是因为您已将您配置TestTextHandler为采用具体Test对象而不是继承您的ITest接口的对象。

您需要将构造函数声明更改为: public TestTextHandler(ITest test)

于 2017-11-15T13:28:13.040 回答
0

我刚刚尝试了上面的代码(加上 IHttpHandler 的接口实现),使用 Habitat 解决方案效果很好。检查以确保程序集正在 /bin 文件夹中发布并且配置正确。(仅供参考:我什至检查了 ShowServicesConfig.aspx 以确保它正在加载并且它的生命周期是瞬态的。)

于 2017-08-03T23:31:43.610 回答