0

After adding code-base configuration class for EF called CustomDbConfiguration I get the 'No parameterless constructor defined for this object' error for my controller called CollectionsController. Once it's removed, everything works fine.

This is my configuration class. It's empty but it doesn't matter in this case.

public class CustomDbConfiguration : System.Data.Entity.DbConfiguration
{
    public CustomDbConfiguration()
    {
    }
}

I'm using StructureMap 4.4.3 as my DI container. This is how controllers registration looks like:

public class ControllerRegistry : Registry
{
    public ControllerRegistry()
    {
        Scan(
            scan =>
            {
                scan.Assembly(".....");
                scan.WithDefaultConventions();
                scan.With(new ControllerConvention());
            });
    }
}

public class ControllerConvention : IRegistrationConvention 
{
    #region Public Methods and Operators

    public void Process(Type type, Registry registry) {
        if (type.CanBeCastTo<Controller>() && !type.IsAbstract) {
            registry.For(type).LifecycleIs(new UniquePerRequestLifecycle());
        }
    }

    public void ScanTypes(TypeSet types, Registry registry)
    {
        var typeList = types.AllTypes();
        foreach (var type in typeList)
        {
            if (type.CanBeCastTo<Controller>() && !type.IsAbstract)
            {
                registry.For(type).LifecycleIs(new UniquePerRequestLifecycle());
            }
        }
    }

    #endregion
}

and this is the controller:

public class CollectionsController : Controller
{
    private readonly INoteService _noteService;
    private readonly IMapperService _mapperService;
    private readonly ISettingsService _settingsService;
    private readonly IPdfService _pdfService;

    public CollectionsController(
        INoteService noteService,
        IMapperService mapperService,
        ISettingsService settingsService,
        IPdfService pdfService)
    {
        _noteService = noteService;
        _mapperService = mapperService;
        _settingsService = settingsService;
        _pdfService = pdfService;
    }
    (...)
}

I'm aware that there is no parametrlees constructor in my controller code but it worked just fine before adding EF configuration class. If I add the missing constructor, I will get different errors complaining about services which are not initialized. I went through all similar questions on SO but I haven't found any solution for this specific problem.

4

0 回答 0