Errors with Managers when Multi-Threading - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Errors with Managers when Multi-Threading

  • Errors with Managers when Multi-Threading
  • 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
  • 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 >>
  • 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.