Errors with Managers when Multi-Threading

Posted by Community Admin on 05-Aug-2018 23:32

Errors with Managers when Multi-Threading

All Replies

Posted by Community Admin on 22-Jun-2011 00:00

Hi,

I'm using a different thread to perform a time consuming task in custom widget. So in that task, I need to use Managers to ready/write content from/to sitefinity. Now the problem is since that logic runs on a different thread I can execute the manager and I'm receiving "Missing current principal" exception. How should I resolve this?

Is there a way to access these managers under a specific user? How to achieve this?

Thanks,
Duneel

Posted by Community Admin on 28-Jun-2011 00:00

Hello Duneel,

One way to do this is to turn off security checks. This is done at the provider level. It is important to turn it on after you complete your operation. So i suggest you use try - finally block.
try

manager.Provider.SuppressSecurityChecks = true;
///Your code///

fiinally

manager.Provider.SuppressSecurityChecks = false;

I also suggest that you manually Dispose() your manager at the end of your background task - since this causes disposing of the ORM resources - database connections.

We also have a static method that you can use to run code with elevated privileges.

SystemManager.RunWithElevatedPrivilege 
This actually simulates the HttpContext as it was called with a system user in admin role. Your code should be put in a delegate that this method calls. It returns the previous state of the HttpContext once the code is finished. Again disposing your managers is important to free db resources.

Regards,
Nikolay Datchev
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

Posted by Community Admin on 30-Nov-2016 00:00

Thanks, SystemManager.RunWithElevatedPrivilege was the key to our multi-threading implementation using Parallel.ForEach.

DataProviderBase.RaiseAfterEvents accesses SystemManager.CurrentHttpContext and that is null in a multi-threaded situation unless running your code under SystemManager.RunWithElevatedPrivilege.

This thread is closed