Copied to clipboard

Flag this post as spam?

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


  • Jeroen Vantroyen 54 posts 394 karma points c-trib
    May 27, 2020 @ 15:17
    Jeroen Vantroyen
    0

    Dependency Injection and IPublishedContentQuery

    Hi,

    if I have a udi with the following value "{umb://any-guid/f91a88d2d4114771ad29afb666eb189c}". Take note of the any-guid because it is of importance and beyond my control (So I can't just change it).

    I can retrieve the document in several ways

    //Using UmbracoHelper --> works
    return Current.UmbracoHelper.Content(udi);
    
    //Using umbracoContext --> throws exception
    contextFactory.EnsureUmbracoContext().UmbracoContext.Content.GetById(udi);
    

    The exception message in the 2nd option is "Udi entity type must be "document"." What is the difference between going through the UmbracoHelper and via the ContentService?

    UmbracoHelper uses the IPublishedContentQuery under the hood, but if I try injecting that, I just get a LightInject-error and Umbraco won't even boot. Injecting the UmbracoHelper also gives me the same error.

    [NullReferenceException: Object reference not set to an instance of an object.]
    Umbraco.Web.Runtime.<>c.<Compose>b__0_6(IFactory factory) in d:\a\1\s\src\Umbraco.Web\Runtime\WebInitialComposer.cs:116
    

    If I cannot inject UmbracoHelper or the IPublishedContentQuery, but I do have the IUmbracoContextFactory, where do I find a Content.GetById that will yield result?

    Any insight?

  • Joep 96 posts 698 karma points
    May 29, 2020 @ 09:19
    Joep
    0

    Hi,

    So the difference between the ContentService and the UmbracoHelper is that you can get the IPublishedContent version from the content cache in the UmbracoHelper and that the ContentService gets a IContent version from the database directly.

    The reason why you can't inject the UmbracoHelper is because it can't create the helper.In order to create the object it needs other objects, but those objects aren't there at the time of startup.

    Try adding a Minimum Runtime level of Run and then inject the Helper. https://our.umbraco.com/Documentation/Implementation/Composing/#runtimelevel

    -Joep

Please Sign in or register to post replies

Write your reply to:

Draft