Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Disabling Umbraco notifications handler by reflection
public class MyUmbracoApplication : UmbracoApplication
{
protected override void OnApplicationStarted(object sender, EventArgs e)
{
base.OnApplicationStarted(sender, e);
// Make sure this is run after the base.OnApplicationStarted!
// The NotificationsHandler in Core is hooked up as a ApplicationEventHandler so we
// can't put this in a handler since we can't control the execution order.
DisableNotificationsHandler();
}
private void DisableNotificationsHandler()
{
// Even though the Published event is on the ContentService it is actually forwarded to a Published event on the internal PublishingStrategy.
// So to get the bound handlers, we need to reflect our way in to the actual PublishingStrategy and get it from there.
var bindingFlags = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy;
var publishingStrategyFieldInfo = typeof(ContentService).GetField("_publishingStrategy", bindingFlags);
if (publishingStrategyFieldInfo == null)
return;
var publishingStrategy = publishingStrategyFieldInfo.GetValue(UmbracoContext.Current.Application.Services.ContentService);
if (publishingStrategy == null)
return;
var publishedFieldInfo = typeof(PublishingStrategy).GetField("Published", bindingFlags);
if (publishedFieldInfo == null)
return;
var publishing = (Delegate)publishedFieldInfo.GetValue(publishingStrategy);
if (publishing == null)
return;
// When we have the bound handlers, we find the NotificationsHandler and unbind it through the ContentService.Published event.
var invocationList = publishing.GetInvocationList();
foreach (var deleg in invocationList)
{
if (deleg.Target == null || !deleg.Target.GetType().FullName.Contains("NotificationsHandler")) continue;
var typedEventHandlerDelegate = deleg as TypedEventHandler<IPublishingStrategy, PublishEventArgs<IContent>>;
if (typedEventHandlerDelegate != null)
{
ContentService.Published -= typedEventHandlerDelegate;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment