Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Sep 23, 2016 @ 08:44
    Michaël Vanbrabandt
    0

    Expose custom context into the applicationevenhandler

    Hi all,

    I have a AppContext in my App.Web project which includes a ServiceContext from my App.Core project.

    My service context contains all of the services to make interactions with my custom database tables.

    I have exposed this AppContext into the HttpContextBase. Something like the UmbracoContext.

    Everything is working fine, I have a UmbracoAuthorizedJsonController where I can call my services and do operations. The problem is when I create my custom events in the App.Core project.

    /// <summary>
    /// Implement custom application events
    /// </summary>
    public class NotelyEvents: ApplicationEventHandler
    {
        // Fired when application is started
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
    
            // .....
    
            // Add events
            ContentService.Deleted += ContentService_Deleted;
            ContentService.EmptiedRecycleBin += ContentService_EmptiedRecycleBin;
        }
    
        // Event fires when clicking on Empty recycle bin
        private void ContentService_EmptiedRecycleBin(IContentService sender, Umbraco.Core.Events.RecycleBinEventArgs e)
        {
            // Check if we are in the content recycle bin
            if(e.IsContentRecycleBin)
            {
                 // I need my ServiceContext here
            }
        }
    
        // Events fires when deleting a node from the recycle bin
        private void ContentService_Deleted(IContentService sender, Umbraco.Core.Events.DeleteEventArgs<Umbraco.Core.Models.IContent> e)
        {
             // I need my ServiceContext here
        }
    }
    

    So basically I need to get my ServiceContext to do some stuff when deleting content nodes.

    How can I do this?

    /Michaël

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Sep 23, 2016 @ 11:24
    Michaël Vanbrabandt
    100

    UPDATE

    Ok I got it working by just creating a new instance of the ServiceContext in each event instead of taking it from the HttpContextBase.

    /Michaël

Please Sign in or register to post replies

Write your reply to:

Draft