+1-888-365-2779
Try Now
More in this section

Forums / Sitefinity SDK / Errors with Managers when Multi-Threading

Errors with Managers when Multi-Threading

3 posts, 0 answered
  1. Duneel
    Duneel avatar
    166 posts
    Registered:
    08 Dec 2010
    22 Jun 2011
    Link to this post
    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
  2. Nikolay Datchev
    Nikolay Datchev avatar
    87 posts
    Registered:
    01 Nov 2016
    28 Jun 2011
    Link to this post
    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 >>
  3. Brandon
    Brandon avatar
    0 posts
    Registered:
    28 Sep 2015
    4 days and 22 hours ago
    Link to this post

    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.

3 posts, 0 answered