Content Area filtered items with Task.Run

Vote:
 

Hello people,

I had come across this scenario and hoping to get an understanding on this. I have a few visitor groups configured and then I have a page with a Content area which holds set of articles.

When I use ContentArea.FilteredItems I get the correct items filtered by users access. However when I have the same ContentArea.FilteredItems in an async task function that uses something like below:

return await Task.Run(() => {

......

var items = ContentArea.FilteredItems ....

}

I only get the items that are marked as Authenticated and visitor groups related filter doesn't work. Samething is with FilterAccess() too. I know that am putting this function on a new thread with Task.Run but not sure if that is the cause of this issue ?

Any inputs or explanation on this would be greatly helpful.

#251001
Mar 19, 2021 15:20
Vote:
 

FilteredItems is using principal identity to do a filtering on the items (fragments of the content area). By default HttpContext based principal resolver should be used. If you are off-loading your work to Task.Run -> there are high chances that code will be executed on thread-pool thread. Which in turn means that HttpContext should be null there (very similar problem that scheduled jobs in Episerver inherit).

One of the quick workaround for your issue that comes to my mind could be - you could get filtered items before you pass work to Task.Run. That would mean that resolving of the items is happening within main thread where you get HttpContext and principal. 

#251027
Mar 20, 2021 19:12
Vote:
 

Valdis,

Thanks for the explanation. I thought the reason could be that task.run creates a new thread and httpcontext might not be available there but wasn't sure myself so thought of asking here.

At present I have changed the implementation and not to use the Task for now as I am loading stuff parallely using AJAX anyway but based on your suggestion I may refactor where i could go back to original solution of using task.run but will extract required filtered items ahead in main thread.

#251047
Mar 20, 2021 23:59
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.