Copied to clipboard

Flag this post as spam?

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


  • Eric Petersson 32 posts 116 karma points
    May 22, 2019 @ 19:59
    Eric Petersson
    0

    Can't register service for RenderMvcController

    Hi

    I am trying to extend my Start Page by doing some controller injections with a custom service I have created for Umbraco 8.

    The code for the start page looks like this:

    public class StartPageController : RenderMvcController
    {
        private readonly ISiteSettingsPageService _siteSettingsPageService;
    
        public StartPageController(ISiteSettingsPageService siteSettingsPageService)
        {
            _siteSettingsPageService = siteSettingsPageService;
        }
    
        public override ActionResult Index(ContentModel model)
        {
            var testing = _siteSettingsPageService.GetRootContent();
            return Index(model);
        }
    }
    

    However, I run into trouble with the ISiteSettingsPageService not being registered.

    So I tried to follow the IoC documentation for the Umbraco 8 documentation with the following registrations:

    public class StartPageComposer : IComposer
    {
        public void Compose(Composition composition)
        {
            composition.Register<SiteSettingsPageService, ISiteSettingsPageService>();
        }
    }
    

    But then I receive "Boot up failure" error message.

    I tried to manually edit the Global.asax.cs (which I needed to add after I had created my project) but don't seem to get a hang of it.

    Does someone know we this keeps failing for me?

    Regards Eric

  • Marc Goodson 945 posts 6285 karma points MVP 4x c-trib
    May 22, 2019 @ 22:03
    Marc Goodson
    100

    Hi Eric

    There is some work in progress docs here with some examples of creating and injecting your own service:

    https://github.com/umbraco/UmbracoDocs/compare/v8/ServicesPattern?short_path=cf2726d#diff-cf2726d2ea2b6f60e0225ce051876a55

    try:

    using Umbraco.Core;
    using Umbraco.Core.Composing;
    using Umbraco8.Services;
    
        namespace YourNamespace.Composers
        {
            public class StartPageComposer : IUserComposer
            {
                public void Compose(Composition composition)
                {         
                 composition.Register<SiteSettingsPageService, ISiteSettingsPageService>(Lifetime.Request);          
                }
            }
        }
    

    It will depend a little on what you are injecting into your SiteSettingsPageService eg something transient UmbracoContext or IPublishedContentQuery...

    which should be fine if you are only using your SiteSettingsPageService in a controller, but if you are also trying to inject it into a Component or ContentFinder where the UmbracoContext is not guaranteed to exist, then that can trigger the boot fail error!

    regards

    Marc

  • Eric Petersson 32 posts 116 karma points
    May 23, 2019 @ 05:42
    Eric Petersson
    0

    Thanks for the reply, Marc

    I do seem to get some kind of boot error for referencing my SiteSettingsPageService from another project?

    enter image description here

  • Eric Petersson 32 posts 116 karma points
    May 23, 2019 @ 05:48
    Eric Petersson
    0

    So the problem seems to still exist even though I swap the service to the same project as the Web project for Umbraco.

    My SiteSettingsPageService looks like this:

    public class SiteSettingsPageService : ISiteSettingsPageService
    {
        private readonly IPublishedContentQuery _contentQuery;
    
        public SiteSettingsPageService(IPublishedContentQuery contentQuery)
        {
            _contentQuery = contentQuery;
        }
    
        public IPublishedContent GetSiteSettingsPage()
        {
            var siteRoot = _contentQuery.ContentAtRoot().FirstOrDefault();
            var siteSettings = siteRoot.Children(f => f.ContentType.Alias == "siteSettingsPage").FirstOrDefault();
            return siteSettings;
        }
    }
    

    And the StartPageController like this:

    public class StartPageController : RenderMvcController
    {
        private readonly ISiteSettingsPageService _siteSettingsPageService;
    
        public StartPageController(ISiteSettingsPageService siteSettingsPageService)
        {
            _siteSettingsPageService = siteSettingsPageService;
        }
    
        public override ActionResult Index(ContentModel model)
        {
            var testing = _siteSettingsPageService.GetSiteSettingsPage();
            return Index(model);
        }
    }
    
  • Eric Petersson 32 posts 116 karma points
    May 23, 2019 @ 06:41
    Eric Petersson
    1

    Okay my mistake, I should of course use the CurrentTemplate rendering for the ActionResult rendering:

    public override ActionResult Index(ContentModel model)
        {
            return CurrentTemplate(model);
        }
    
  • Marc Goodson 945 posts 6285 karma points MVP 4x c-trib
    May 23, 2019 @ 08:51
    Marc Goodson
    0

    Was just trying it out locally 'as it all looks right' but yes CurrentTemplate!

    So all working for you now? (it's like magic when it works!)

    regards

    Marc

  • Eric Petersson 32 posts 116 karma points
    May 23, 2019 @ 09:30
    Eric Petersson
    0

    Yes, nowadays Umbraco feels like a fresh and modern back end solution working with

Please Sign in or register to post replies

Write your reply to:

Draft