Kentico TreeProvider and MultiThreading

Matt Hobbs asked on January 12, 2019 19:14

Hello, Just a quick question on the API and retrieving pages while multi threading.

I have a simple Parallel.ForEach loop that retrieves a page from the tree based on the information being looped. This works fine on 1 thread, but as soon as you try run this over multiple threads you get a Object not set to the instance of an object exception.

Below is the piece of code that causes the exception, the tree variable sets fine, but when I try and set the page variable I get the error. All variables used in the query have the correct values, same as when its run on 1 thread.

var tree = new TreeProvider(MembershipContext.AuthenticatedUser);

        var page = tree.SelectNodes(className)
            .Published()
            .OnCurrentSite()
            .Culture(cultureCode)
            .FirstOrDefault();

        return page;

Does the kentico tree API allow multi threading? or do I need to call something different to achieve this?

Thanks

Correct Answer

Matt Hobbs answered on January 14, 2019 11:14

The solution to this was the site context needed to be reinitialised per a thread.

If you set the site and site context on the main thread these will need passing to each thread or re initialising.

0 votesVote for this answer Unmark Correct answer

   Please, sign in to be able to submit a new answer.