Copied to clipboard

Flag this post as spam?

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


  • David Armitage 510 posts 2082 karma points
    Jun 07, 2021 @ 14:11
    David Armitage
    0

    Umbraco 9 Registering a Service and Exposing it to Dependency Injection

    Hi All,

    Does anyone know how to convert this code to Umbraco 9.

    I am trying to compose a service I have created called SiteHelper: ISiteHelper so I can then use it in my controllers using DI

    public class SiteHelperComposer : IUserComposer
    {
            public void Compose(Composition composition)
            {
                composition.Register<ISiteHelper, SiteHelper>(Lifetime.Singleton);
            }
    }
    

    Here is an example of my service I am trying to compose.

    public interface ISiteHelper
    {
            string TestMethod();
    }
    
    public class SiteHelper : ISiteHelper
    {
        private readonly ILogger _logger;
    
        public SiteHelper(ILogger logger)
        {
            _logger = logger;
        }
    
        public string TestMethod()
        {
            try
            {
                throw new Exception();
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Index | Exception: {0} | Message: {1} | Stack Trace: {2}", e.InnerException != null ? e.InnerException.ToString() : "", e.Message != null ? e.Message.ToString() : "", e.StackTrace);
            }
    
            return "This returned a value";
        }
    }
    

    I have been playing around with the following code in V9 but haven't got it to work.

    builder.Services.AddSingleton<ISiteHelper, SiteHelper>();
    

    I kind of had some success with this if I got rid of the logger but couldn't get it to work including the logger or other dependencies.

    builder.Services.AddSingleton<ISiteHelper>((s) =>
    {
           return new SiteHelper();
    });
    

    Kind Regards

    David

  • Mathias Hove 11 posts 74 karma points
    Jun 07, 2021 @ 16:28
    Mathias Hove
    100
    public class SiteHelper : ISiteHelper
    {
        private readonly ILogger<SiteHelper> _logger;
    
        public SiteHelper(ILogger<SiteHelper> logger)
        {
               _logger = logger;
        }
    
        public string TestMethod()
        {
            try
            {
                throw new Exception();
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Index | Exception: {0} | Message: {1} | Stack Trace: {2}", e.InnerException != null ? e.InnerException.ToString() : "", e.Message != null ? e.Message.ToString() : "", e.StackTrace);
            }
    
            return "This returned a value";
        }
    }
    

    this should fix it :)

    Explained here https://stackoverflow.com/questions/52921966/unable-to-resolve-ilogger-from-microsoft-extensions-logging/57590076#57590076

  • Mathias Hove 11 posts 74 karma points
    Jun 09, 2021 @ 05:42
    Mathias Hove
    0

    Did it work out for you? :)

  • David Armitage 510 posts 2082 karma points
    Jun 09, 2021 @ 06:09
    David Armitage
    0

    I haven't tried yet. I will have a look this evening when tuning in to code garden.

    usually just work on this stuff a couple of hours on a night or on a weekend. I will let you know how I get on.

  • David Armitage 510 posts 2082 karma points
    Jun 09, 2021 @ 11:07
    David Armitage
    0

    Hi Mathias,

    Thanks for helping me get that working. You was correct the composing was failing because of the ILogger was injected inforrectly.

    For completion here is my code.

    using System;
    using Umbraco.Cms.Core.Composing;
    using Microsoft.Extensions.Logging;
    using Umbraco.Cms.Core.DependencyInjection;
    using Microsoft.Extensions.DependencyInjection;
    
    namespace Website.Core.Helpers
    {
        public class SiteHelperComposer : IUserComposer
        {
            public void Compose(IUmbracoBuilder builder)
            {
                builder.Services.AddSingleton<ISiteHelper, SiteHelper>();
            }
        }
    
        public interface ISiteHelper
        {
            string TestMethod();
        }
    
        public class SiteHelper : ISiteHelper
        {
            private readonly ILogger<SiteHelper> _logger;
    
            public SiteHelper(ILogger<SiteHelper> logger)
            {
                _logger = logger;
            }
    
            public string TestMethod()
            {
    
                try
                {
                    throw new Exception();
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "Index | Exception: {0} | Message: {1} | Stack Trace: {2}", e.InnerException != null ? e.InnerException.ToString() : "", e.Message != null ? e.Message.ToString() : "", e.StackTrace);
                }
    
                return "This returned a value";
            }
        }
    }
    

    Thanks again.

    David

  • David Armitage 510 posts 2082 karma points
    Jun 09, 2021 @ 12:14
    David Armitage
    1

    I actually wrote a more in depth blog article on this subject if anyone is interested.

    https://www.umbrajobs.com/blog/posts/2021/june/how-to-compose-a-custom-service-in-umbraco-9/

    hope this info helps someone. Feel free to comment here if you need further help.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies