Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Gary Cheetham 20 posts 144 karma points
    May 25, 2016 @ 16:19
    Gary Cheetham
    0

    Which types to register with IOC container so ContentService can be instantiated

    One of my classes depends on IContentService. I'm trying to provide an IContentService to this class with an IOC container.

    Here is how I'm registering IContentService with my IOC (Autofac)

    builder.RegisterType<ContentService>().As<IContentService>();
    

    However, Autofac doesn't know about every type Umbraco.Core.Services.ContentService needs to instantiate.

    public ContentService(IDatabaseUnitOfWorkProvider provider, RepositoryFactory repositoryFactory, ILogger logger, IEventMessagesFactory eventMessagesFactory, IPublishingStrategy publishingStrategy, IDataTypeService dataTypeService, IUserService userService);
    

    I'm running into stack traces like this one:

    None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type
    'Umbraco.Core.Services.ContentService' can be invoked with the available services and parameters:Cannot resolve
    parameter 'Umbraco.Core.Persistence.RepositoryFactory repositoryFactory' of constructor 'Void
    .ctor(Umbraco.Core.Persistence.UnitOfWork.IDatabaseUnitOfWorkProvider, Umbraco.Core.Persistence.RepositoryFactory,
    Umbraco.Core.Logging.ILogger, Umbraco.Core.Events.IEventMessagesFactory, Umbraco.Core.Publishing.IPublishingStrategy,
    Umbraco.Core.Services.IDataTypeService, Umbraco.Core.Services.IUserService)'.
    

    Which types do I register with my IOC to satisfy the dependencies of ContentService ?

  • Gary Cheetham 20 posts 144 karma points
    May 26, 2016 @ 09:14
    Gary Cheetham
    3

    I've managed to find the right way to do this.

    Umbraco creates an instance of ContentService for you with the correct configuration.

    Therefore, it's better to register this instance with Autofac by using RegisterInstance() instead of trying to register the ContentService type with RegisterType().

    builder.RegisterInstance(applicationContext.Services.ContentService).As<IContentService>();
    

    This way, Autofac won't try to instantiate ContentService itself - so it can't mess up the instantiation.

Please Sign in or register to post replies

Write your reply to:

Draft