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.
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.)
So the questions are: how do I get the typed Member or how do I inject the MembershipHelper in a Task/IComponent?
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
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);
Injecting MembershipHelper in Task (not working)
Hello!
TL;DR: how do I get a typed
Member
in a, or how do I inject theMembershipHelper
in aTask
/IComponent
?I'm trying to get typed members in a
Task
, but any examples here are only onCurrent.UmbracoHelper.MembershipHelper
, whitch is deprecated (to the extent that is casts a null exception) or forUmbracoContextFactory.UmbracoContext.Content.GetById
which returns null for a member.When I try to do as described (as with lots of other components) to inject the
MembershipHelper
in theIComponent
Umbraco fails to start, with YSOD... (No other info in the log file.)So the questions are: how do I get the typed
Member
or how do I inject theMembershipHelper
in aTask
/IComponent
?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
Then you might be able to query Members via the NuCache PublishedSnapshot of the UmbracoContext eg
https://github.com/umbraco/Umbraco-CMS/blob/8efdc5a047a37d3a4cc79de4dbda02a7754d7adc/src/Umbraco.Web/PublishedCache/NuCache/PublishedSnapshot.cs#L64
If that's not null too!!!
regards
marc
is working on a reply...