Skip to content

Instantly share code, notes, and snippets.

@clausjensen
Last active February 10, 2018 02:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clausjensen/d178b5f639f2ba167a4913f7d3c0b1bc to your computer and use it in GitHub Desktop.
Save clausjensen/d178b5f639f2ba167a4913f7d3c0b1bc to your computer and use it in GitHub Desktop.
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