Copied to clipboard

Flag this post as spam?

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


  • Ulf Möllerström 71 posts 248 karma points
    Sep 08, 2021 @ 10:45
    Ulf Möllerström
    0

    Injecting MembershipHelper in Task (not working)

    Hello!

    TL;DR: how do I get a typed Member in a, or how do I inject the MembershipHelper in a Task/IComponent?


    I'm trying to get typed members in a Task, but any examples here are only on Current.UmbracoHelper.MembershipHelper, whitch is deprecated (to the extent that is casts a null exception) or for UmbracoContextFactory.UmbracoContext.Content.GetById which returns null for a member. enter image description hereenter image description here

    When I try to do as described (as with lots of other components) to inject the MembershipHelper in the IComponent Umbraco fails to start, with YSOD... (No other info in the log file.) enter image description here

    So the questions are: how do I get the typed Member or how do I inject the MembershipHelper in a Task/IComponent?

  • Marc Goodson 2157 posts 14435 karma points MVP 9x c-trib
    Sep 12, 2021 @ 13:09
    Marc Goodson
    1

    Hi Ulf

    Yes if something relies on UmbracoContext, eg UmbracoHelper or MembershipHelper, you can only inject them into places that will have an existing UmbracoContext, eg a Controller etc

    https://our.umbraco.com/documentation/Implementation/Services/#accessing-core-services-and-helpers-when-there-is-no-umbracocontext-eg-in-a-component-or-c-class

    Anything like a background task or component will fail injecting these things into as the UmbracoContext is not guaranteed to exist.

    There is an article here that might be helpful?

    https://www.gitmemory.com/issue/umbraco/Umbraco-CMS/6335/530831406

    That sort of hints that if you are able to construct an UmbracoContext eg

    https://our.umbraco.com/documentation/Implementation/Services/#accessing-published-content-outside-of-a-http-request

    using the UmbracoContextFactory and calling EnsureUmbracoContext

     using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext()){
     var umbracoContext = umbracoContextReference.UmbracoContext;    
    }
    

    Then you might be able to query Members via the NuCache PublishedSnapshot of the UmbracoContext eg

    IPublishedMemberCache memberCache = umbracoContext.PublishedSnapshot.Members;
    var member = memberCache.GetById(123);
    

    https://github.com/umbraco/Umbraco-CMS/blob/8efdc5a047a37d3a4cc79de4dbda02a7754d7adc/src/Umbraco.Web/PublishedCache/NuCache/PublishedSnapshot.cs#L64

    If that's not null too!!!

    regards

    marc

  • 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