Start up handler - ContentService_Published calling in loop
Hi
My code is like that but when i published or create any node it is calling ContentService_Published infinite times. Can someone please suggest what i am doing wrong here.
public class ConfigurePublishedContentModelFactory : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
var types = PluginManager.Current.ResolveTypes<PublishedContentModel>();
var factory = new PublishedContentModelFactory(types);
PublishedContentModelFactoryResolver.Current.SetFactory(factory);
}
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
Umbraco.Core.Services.ContentService.Published += ContentService_Published;
}
void ContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
{
var contentService = ApplicationContext.Current.Services.ContentService;
var content = contentService.GetById(e.PublishedEntities.FirstOrDefault().Id);
content.SetValue("bannerTitle", "This text w");
contentService.Save(content);
contentService.Publish(content);
}
}
Still, if you want to prevent calling an eventhandler more than once, you must make sure that you are not triggering it again in the eventhandler yourselves.
Start up handler - ContentService_Published calling in loop
Hi
My code is like that but when i published or create any node it is calling ContentService_Published infinite times. Can someone please suggest what i am doing wrong here.
Manish
I just wanted to call only a new node is created or existing is published.
Calling Publish in the Published eventhandler, will again trigger a Published event and will call your ContentService_Published again.
Without any conditional statements there's nothing that will prevent calling Publish again and again.
So make sure that if you really need to call Publish in your Published event handler, the next call for this same item does not call Publish.
A solution to prevent infinite calls, is by checking for the content of a particular property.
In your example, if bannerTitle already contains the value "This text w" then do not call Publish.
Thanks for your reply
But is there any other solution which allow only one call
Manish
The possible events that you can use can be found here:
Content Service Events
Still, if you want to prevent calling an eventhandler more than once, you must make sure that you are not triggering it again in the eventhandler yourselves.
Many thanks
Micha Somers
is working on a reply...
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.