I'm new to Sitecore. v9.01

I have a class with constructor as below:

public Filter(IFilterHelper filterHelper)
{
    _filterHelper = filterHelper;
}

When I publish the site I get the error that it cannot find the parameterless constructor. I have set up all the DI.

If I add a parameterless constructor:

public Filter()
{
}

The site runs. I break point on my original constructor public Filter(IFilterHelper filterHelper) and navigate to the page - we hit the breakpoint, so the DI is working.

Is there something in Sitecore that needs a parameterless constructor? Or could there be some reference to it that only causes run time errors?

To set up the DI I'm using the Sitecore.DependencyInjection.IServicesConfigurator and configuring like so:

public void Configure(IServiceCollection serviceCollection)
{
    serviceCollection.AddTransient<IFilterHelper, FilterHelper>();
    //others...
}
  • If this is a class - what and how are you injecting that dependency into? A controller? Pipeline processor? – Richard Seal Dec 6 at 14:56
  • Probably there is no instance of IFilterHelper in the DI. So it cannot call the constructor with the IFilterHelper. You can get an overview off the dependency injection config on /sitecore/admin/ShowServicesConfig.aspx – Bart Verdonck Dec 6 at 15:42
  • But I can set a breakpoint on that constructor and it gets hit – Rick Dec 6 at 15:45
  • Are you using AddMvcControllers method from this gist.github.com/kamsar/460528edc3043b16bdccfdbd0ea0c552 to setup DI? – Jeroen Dec 6 at 20:22
  • @Jeroen I've just added some info to the question about this. Let me know if it doesn't answer the question – Rick Dec 7 at 8:29

I have experienced the same problem before as well. I have managed to make this work by using Poor Man's Dependency Injection.

    private readonly IFilterHelper _filterHelper;

    public Filter() : this(ServiceLocator.ServiceProvider.GetService<IFilterHelper>())
    {           
    }

    public Filter(IFilterHelper filterHelper)
    {
        _filterHelper = filterHelper;
    }

If the above doesn't work, then you will just have to new up an instance of filterHelper.

    private readonly IFilterHelper _filterHelper;

    public Filter() : this(new FilterHelper())
    {           
    }

    public Filter(IFilterHelper filterHelper)
    {
        _filterHelper = filterHelper;
    }

The above will resolve your issues, but I just don't know why sometimes the ServiceLocator parameter method does not work and I am forced to new up an instance of a class. By then, I ran out of time to investigate and just had to deliver the work.

If you actually reflect the DLL of showservicesconfig.aspx, it's a bunch of lies and #FakeNews that only shows the DI registration via the ServiceCollections. Just because it is displayed on that page, it does not mean that usage of DI works anywhere. It is just a good place as a sanity check to ensure the registration is done.

  • The parameterless version never seems to get called, not during testing anyway, it just needs to be there or sitecore blows up - it's looking for the parameterless constructor when the site loads but I can't figure out why. – Rick Dec 7 at 15:37
  • Ah, I thought you were having trouble running inside a pipeline processor. – Vincent Lui Dec 7 at 20:03

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.