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
    Jun 09, 2017 @ 08:49
    Michaël Vanbrabandt
    0

    ApplicationContext will be removed from the UmbracoContext

    Hi all,

    I was going through the source code and saw the following:

            /// <summary>
            /// Gets the current ApplicationContext
            /// </summary>
            [UmbracoWillObsolete("Do not access the ApplicationContext via the UmbracoContext, either inject the ApplicationContext into the services you need or access it via it's own Singleton accessor ApplicationContext.Current")]
            [EditorBrowsable(EditorBrowsableState.Never)]
            public ApplicationContext Application { get; private set; }
    

    Why will this be removed from the UmbracoContext and how will this effect the Umbraco in the future?

    Because it says that we need to inject the ApplicationContext into the services.

    /Michaël

  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    Jun 17, 2017 @ 22:34
    Marc Goodson
    4

    Hi Michael

    I think it's to do with the scope/lifespan of objects - you can read more in Shannon's common pitfalls post:

    https://our.umbraco.org/documentation/reference/Common-Pitfalls/

    Essentially ApplicationContext is scoped for the lifetime of the application and is shared by all threads.

    Whereas UmbracoContext is based on HttpContext bound to the current Http Request and can't be shared between threads.

    So the obsoletion is just to make the distinction clearer, eg people may get used to accessing the ApplicationContext via UmbracoContext.Current.Application, however this won't work in a non HttpRequest context, eg a background task (gathering nodes event etc)

    So the advice is, if you are in an Umbraco base class eg Surface Controller, use the Services. property as a gateway to Umbraco's api services...

    var contentService = Services.ContentService;
    

    .. and if outside of a special Umbaco base class, maybe some sort of custom service perhaps that needs to use the Umbraco Api Services, then construct your custom service class, so that the api service you need can be passed into it via the constructor (injecting the dependency)

    public class MySuperUpdateService {

    IContentService _contentService;
    
    public MySuperUpdateService(IContentService contentService) {
    _contentService = contentService;
    }
    

    }

    or if you have to use singletons to access these contexts, then use their own constructs to do so eg

    UmbracoContext.Current or ApplicationContext.Current

    and not

    UmbracoContect.Current.Application.

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Jun 19, 2017 @ 09:30
    Michaël Vanbrabandt
    0

    Hi marc,

    thank you for this documentation! Its now clear to me!

    Have a nice day

    /Michaël

  • MuirisOG 382 posts 1284 karma points
    Jun 19, 2017 @ 13:40
    MuirisOG
    0

    This is an excellent link, Marc.

    It looks like I have a bit of rewriting to do, as I think I've fallen for every pitfall in the book!

    I note that there is an acknowledgement that the template editor in the back office needs changing (this is where I got a lot of my sample code).

    I also note that there is an acknowledgement that people are using Umbraco for what should actually be stuck into separate database tables. I've been thinking this for some time.

    Many thanks

    Muiris

Please Sign in or register to post replies

Write your reply to:

Draft