Copied to clipboard

Flag this post as spam?

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


  • Niels Swimberghe 17 posts 99 karma points c-trib
    Oct 25, 2021 @ 15:35
    Niels Swimberghe
    0

    Should you always dispose of UmbracoContext?

    I recently moved from Umbraco 8 to Umbraco 9 and had to refactor my code to use the IUmbracoContextAccessor more often. This may be a dumb question, but I wanted to make sure.
    When getting an Umbraco context from IUmbracoContextAccessor, should we dispose of it or will it be handled elsewhere at the end of the request life cycle?

  • Corné Hoskam 23 posts 291 karma points
    Oct 28, 2021 @ 12:23
    Corné Hoskam
    1

    Hi Niels!

    Core services like the IUmbracoContextAccessor (but also things like the UmbracoHelper, IPublishedContentQuery, ...) are all based on an HttpRequest, and their lifetime is controlled by it. This also means that they will automatically be disposed of when the request itself ends! The same reason applies for why we cannot inject these services if we are not operating within a request!

    Hope this helped!

    Corné

Please Sign in or register to post replies

Write your reply to:

Draft