Use "UmbracoHelper" outside the controller, in a "Components" class
Hi everyone,
I'm trying to use the "UmbracoHelper" in a "Components" class, inside a notification handler class (here an example of the new notification handler, and here a video).
In Umbraco 8 was possible to use the static class "Umbraco.Web.Composing.Current.UmbracoHelper" to retrieve published content, for example:
var root = umbracoHelper.ContentAtRoot().ToList().DescendantsOrSelf<Root>().FirstOrDefault()
I'm trying to get the "UmbracoHelper" class via dependency injection, but it seems that it's not possible to get it. For example, if you add the constructor to this DontShout class, with UmbracoHelper in DI, you will obtain an exception.
How can I obtain "UmbracoHelper" outside the controller in Umbraco 9?
Not sure if you've got the same issue I found but if you do need the latter approach, I explained a little about the reasons why this latter approach is needed under "Handing Different Lifetimes of Dependencies" here.
I have tried to inject IPublishedContentQuery but it seems that is not present in DI, and it returns this exception when I save the page:
An error occurred
Cannot resolve 'System.Collections.Generic.IEnumerable'1[Umbraco.Cms.Core.Events.INotificationHandler'1[Umbraco.Cms.Core.Services.Notifications.ContentSavingNotification]]'
from root provider because it requires scoped service
'Umbraco.Cms.Core.IPublishedContentQuery'.
Instead, using the IHttpContextAccessor I was able to get the UmbracoHelper:
Thank you for referencing your blog, the article was useful, and yes I have got your same issue.
I'm also curious to read something about the available services in DI and the difference between the services, for example what kind of services .AddBackOffice() and .AddWebsite() will make available? And what's the difference between AddBackOfficeCore() and AddUmbracoCore()?
IUmbracoHelperAccessor is another choice to use in constructor.
private readonly IUmbracoHelperAccessor _umbracoHelperAccessor;
public class TestClass
{
private readonly IUmbracoHelperAccessor _umbracoHelperAccessor;
public TestClass(IUmbracoHelperAccessor umbracoHelperAccessor)
{
_umbracoHelperAccessor = umbracoHelperAccessor;
}
public void Test()
{
if (_umbracoHelperAccessor.TryGetUmbracoHelper(out var umbracoHelper) is false)
{
return;
}
var rootNode = umbracoHelper.ContentAtRoot().FirstOrDefault();
}
}
Use "UmbracoHelper" outside the controller, in a "Components" class
Hi everyone,
I'm trying to use the "UmbracoHelper" in a "Components" class, inside a notification handler class (here an example of the new notification handler, and here a video).
In Umbraco 8 was possible to use the static class "Umbraco.Web.Composing.Current.UmbracoHelper" to retrieve published content, for example:
I'm trying to get the "UmbracoHelper" class via dependency injection, but it seems that it's not possible to get it. For example, if you add the constructor to this DontShout class, with UmbracoHelper in DI, you will obtain an exception.
How can I obtain "UmbracoHelper" outside the controller in Umbraco 9?
Thank you
I think here you can inject
IPublishedContentQuery
, which is whatUmbracoHelper
defers for theContentAtRoot()
method.If it doesn't work injecting directly, try instead injecting
IHttpContextAccessor
and resolving from:Not sure if you've got the same issue I found but if you do need the latter approach, I explained a little about the reasons why this latter approach is needed under "Handing Different Lifetimes of Dependencies" here.
Andy
Dear Andy,
thank you for your reply.
I have tried to inject
IPublishedContentQuery
but it seems that is not present in DI, and it returns this exception when I save the page:Instead, using the
IHttpContextAccessor
I was able to get the UmbracoHelper:Thank you for referencing your blog, the article was useful, and yes I have got your same issue.
I'm also curious to read something about the available services in DI and the difference between the services, for example what kind of services
.AddBackOffice()
and.AddWebsite()
will make available? And what's the difference betweenAddBackOfficeCore()
andAddUmbracoCore()
?Thank you
IUmbracoHelperAccessor is another choice to use in constructor.
this sounds the same question as my earlier thread if that helps https://our.umbraco.com/forum/umbraco-9//106681-umbraco-context-in-viewcomponent-best-practice
try this link https://our.umbraco.com/documentation/implementation/Services/ and works with me
is working on a reply...