Copied to clipboard

Flag this post as spam?

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


  • Oumarou Dicko 5 posts 85 karma points
    May 23, 2019 @ 16:44
    Oumarou Dicko
    0

    Umbraco 8 - Access to custom service from view

    Hello, I have a custom service and i am using the interface IUserComposer in order to register this service.

     public class RegisterGiroCustomService : IUserComposer
     {
        public void Compose(Composition composition)
        {
            composition.RegisterFor<IGiroContentService, GiroContentService>();
        }
     }
    

    I am wondering how i can access to my custom service (IGiroContentService) from a partial view?

    Thanks

  • Andrey 3 posts 44 karma points
    May 24, 2019 @ 21:40
    Andrey
    101

    Hi, may be it will be helpful

    var service = DependencyResolver.Current.GetService<IGiroContentService>();
    
  • Oumarou Dicko 5 posts 85 karma points
    May 27, 2019 @ 08:13
    Oumarou Dicko
    0

    Thank you very much. This is exactly what i was looking for.

  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    May 25, 2019 @ 20:32
    Marc Goodson
    2

    Hi Oumarou

    For neatness, and if you are using a service in lots of different views then you could create your own version of UmbracoViewPage... and wire things up once...

    There is some WIP documentation in progress here:

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

    that has the following example of how to do this:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Umbraco.Core;
    using Umbraco.Core.Cache;
    using Umbraco.Core.Composing;
    using Umbraco.Core.Services;
    using Umbraco.Web.Mvc;
    using Umbraco8.Services;
    using Current = Umbraco.Web.Composing.Current;
    
    namespace Umbraco8.ViewPages
    {
        public abstract class CustomViewPage<T> : UmbracoViewPage<T>
        {
            public readonly ISiteService SiteService;
            public CustomViewPage() : this(
                Current.Factory.GetInstance<ISiteService>(), 
                Current.Factory.GetInstance<ServiceContext>(),
                Current.Factory.GetInstance<AppCaches>()
                )
            {
            }
            public CustomViewPage(ISiteService siteService, ServiceContext services, AppCaches appCaches)
            {
                SiteService = siteService;
                Services = services;
                AppCaches = appCaches;
            }
    
            protected override void InitializePage()
            {
                base.InitializePage();
            }
        }
        public abstract class CustomViewPage : UmbracoViewPage
        {
            public readonly ISiteService _siteService;
            public CustomViewPage() : this(
                Current.Factory.GetInstance<ISiteService>(),
                Current.Factory.GetInstance<ServiceContext>(),
                Current.Factory.GetInstance<AppCaches>()
                )
            { }
    
                public CustomViewPage(ISiteService siteService, ServiceContext services, AppCaches appCaches)
            {
                _siteService = siteService;
                Services = services;
                AppCaches = appCaches;
            }
    
            protected override void InitializePage()
            {
                base.InitializePage();
            }
        }
    }
    

    and then in your view

    @using Umbraco8.ViewPages
    @inherits CustomViewPage
    @{
    
        Layout = "master.cshtml";
        IPublishedContent newsSection = SiteService.GetNewsSection();
    }
    <section class="section">
        <div class="container">
            <article>
    

    Anyway it's just a thought... the key to it is anywhere you can get the current reference from a service that's registered with DI by using:

    using Current = Umbraco.Web.Composing.Current;
     Current.Factory.GetInstance<ISiteService>()
    

    if that helps

    regards

    Marc

  • Oumarou Dicko 5 posts 85 karma points
    May 27, 2019 @ 08:13
    Oumarou Dicko
    0

    Thank you. i like your approach.

Please Sign in or register to post replies

Write your reply to:

Draft