V8: Can't Save and Publish Content during startup due to notifications issue
See original GitHub issueI’m attempting to create content, then save and publish using the content service but I’m running in to a null reference exception.
I’m not sure why this is happening, my hunch at the moment it is that I’m trying to create it from a composer and wondering if maybe something isn’t initialised for me at that point.
Anyway, here’s the code I’m using (or at least, the important parts):
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Models;
namespace Test
{
[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
public class ContentComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.Components().Append<ContentComponent>();
}
}
public class ContentComponent : IComponent
{
public void Initialize()
{
IContent homepageNode = Current.Services.ContentService.GetRootContent().FirstOrDefault(x => x.ContentType.Alias == "homepage");
if (homepageNode == null)
{
IContent homepage = Current.Services.ContentService.Create("Homepage", -1, "homepage");
Current.Services.ContentService.SaveAndPublish(homepage);
}
}
public void Terminate() { }
}
}
When I attempt to do this, I get the following error:
Object reference not set to an instance of an object.
And the stack trace:
at Umbraco.Web.Routing.RedirectTrackingComponent.get_Moving()
at Umbraco.Web.Routing.RedirectTrackingComponent.get_LockedEvents()
at Umbraco.Web.Routing.RedirectTrackingComponent.ContentService_Publishing(IContentService sender, PublishEventArgs`1 args)
at Umbraco.Core.Events.QueuingEventDispatcherBase.DispatchCancelable[TSender,TArgs](TypedEventHandler`2 eventHandler, TSender sender, TArgs args, String eventName)
at Umbraco.Core.Services.Implement.ContentService.StrategyCanPublish(IScope scope, IContent content, Boolean checkPath, IReadOnlyList`1 culturesPublishing, IReadOnlyCollection`1 culturesUnpublishing, EventMessages evtMsgs, ContentSavingEventArgs savingEventArgs)
at Umbraco.Core.Services.Implement.ContentService.CommitDocumentChangesInternal(IScope scope, IContent content, ContentSavingEventArgs saveEventArgs, Int32 userId, Boolean raiseEvents, Boolean branchOne, Boolean branchRoot)
at Umbraco.Core.Services.Implement.ContentService.SaveAndPublish(IContent content, String culture, Int32 userId, Boolean raiseEvents)
at Test.ContentComponent.Initialize() in C:\Users\benpa\Desktop\DELETE ME\Test\Test\ContentComponent.cs:line 27
at Umbraco.Core.Composing.ComponentCollection.Initialize()
at Umbraco.Core.Runtime.CoreRuntime.Boot(IRegister register, DisposableTimer timer)
Reproduction
Specifics
Umbraco Version: 8.0.1
Steps to reproduce
- Set up fresh U8 install
- Login, create new doc type with alias of “homepage” (allowed at root)
- Set up component (code posted above)
- Build, run (after site restart to ensure component code runs)
Expected result
Homepage node created at root of content site.
Actual result
Null reference exception thrown
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (8 by maintainers)
Top Results From Across the Web
tomcat - Error in publishing the configuration in Eclipse
Publishing to tomcat v6.0 server at localhost..'has encountered a problem. Publishing the configuration.. tomcat give above error. How to ...
Read more >Notification – Custom Notifications and Alerts for WordPress
Set unlimited Notifications in your WordPress Admin via the beautiful and ... (saved as a draft) notification; Updated post notification; Post send for ......
Read more >Errors in error.log on both Author & Publish Instances during ...
Analyzing the error logs, it has been noticed that at the time the errors appeared, the instances were still in Startup mode, not...
Read more >How to debug SegmentNotFoundException when Issue is ...
How to fix corruption when encountering “ConsistencyChecker - No good revision found” on consistency check · Stop AEM. · Repeat step 3 for...
Read more >v8.0.0
This is the main Umbraco download, generally you won't need anything else. Downloaded 4888 times - uploaded 26 February 2019. Also available on...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
What happens is this: the
RedirectTrackingComponent
uses theCurrent.UmbracoContext.HttpContext
to store some state items that need to be carried over the differentContentService
event handlers. In aIComponent
when runningInitialize
, since there is no current request, there is noUmbracoContext
, and it all ends in aNullReferenceException
.Turns out there is a way to ensure there is a context. Can you try with the following code?
Two things to note:
HttpContext
to hold some context… and we are slowly getting rid of them as they create an unwanted dependency toSystem.Web
.I’m facing the same issue, I’m trying to import some data from a API using a BackgroundTaskRunner. The weird part is the save and publish does work at my end it just throws an error.
at LightInject.Web.PerWebRequestScopeManager.GetOrAddScope() at LightInject.Web.PerWebRequestScopeManager.get_CurrentScope() at LightInject.ServiceContainer.GetInstance(Type serviceType) at Umbraco.Core.Composing.LightInject.LightInjectContainer.GetInstance(Type type) at Umbraco.Core.FactoryExtensions.GetInstance[T](IFactory factory) at Umbraco.Web.Runtime.WebInitialComposer.<>c.<Compose>b__0_6(IFactory factory) at Umbraco.Core.Composing.LightInject.LightInjectContainer.<>c__DisplayClass20_01.<Register>b__0(IServiceFactory f) at LightInject.ServiceContainer.GetInstance(Type serviceType) at Umbraco.Core.Composing.LightInject.LightInjectContainer.GetInstance(Type type) at Umbraco.Core.FactoryExtensions.GetInstance[T](IFactory factory) at Umbraco.Deploy.Cloud.LiveEditing.EventSubscriber.GetHubAndHelper(IHubContext& hub, UmbracoHelper& helper) at Umbraco.Deploy.Cloud.LiveEditing.EventSubscriber.ContentService_Published(IContentService sender, PublishEventArgs
1 e)`