locked
Entity_CanUpdate throws 'Save operation failed', It is not valid to access this object on the current thread RRS feed

  • Question

  • I am trying to call some custom code to figure out the _CanUpdate, _CanInsert permissions on the  server side entities.

    Here is the error:

    

    The code that works:

    partial void Countries_CanUpdate(ref bool result)
           {
               result=true;
           }

    Code that throws error:

    partial void Countries_CanUpdate(ref bool result)
            {
                result =  GenericHasAccess("Countries_CanUpdate");
            }

    I have also tried

    partial void Countries_CanUpdate(ref bool result)
    {
        GenericHasAccess("Countries_CanUpdate"ref result);
    }
    

    Both result in same error. 

    Wednesday, October 29, 2014 8:00 PM

Answers

  • I solved the issue by creating a temporary class to hold the value across threads like this:

       

    public class boolClass    {        public bool isTrue;    }

                        
    private void GenericHasAccess(string permission, boolClass boolclass)
          {
              this.Details.Dispatcher.BeginInvoke(() =>
               {
    var currentUser = ServerApplicationContext.Current.Application.User;
                       string s = System.Web.Security.Roles.ApplicationName;
                       RoleProvider provider = System.Web.Security.Roles.Provider;
                       boolclass.isTrue = provider.IsUserInRole(currentUser.Identity.Name, permission);
               });
          }

    partial void MyEntity_CanUpdate(ref bool result)
          {
              boolClass bc = new boolClass();
              GenericHasAccess("MyEntity_CanUpdate", bc);
              result = bc.isTrue;
          }
    



    • Edited by CostasZ Tuesday, November 4, 2014 2:31 PM
    • Marked as answer by CostasZ Tuesday, November 4, 2014 2:32 PM
    Tuesday, November 4, 2014 2:31 PM

All replies

  • please post method body of GenericHasAccess.

    paul van bladel ==independent enterprise application architect== http://blog.pragmaswitch.com

    Thursday, October 30, 2014 6:39 AM
  • I wrote my own AspNetRoleProvider to go against our homegrown security model that is used throughout the enterprise.

    I am assuming this happens in another thread. I am not sure how to synchronize a ref parameter though. From what I've read, it's impossible.

    private void GenericHasAccess(string permission, ref bool result)

            {             using (ServerApplicationContext ctx = ServerApplicationContext.Current)             {                 var currentUser = ctx.Application.User;                 string s = System.Web.Security.Roles.ApplicationName;                 RoleProvider provider = System.Web.Security.Roles.Provider;                 result = provider.IsUserInRole(currentUser.Identity.Name, permission);             }         }

      

    Here's the other version that returns a bool

    private bool GenericHasAccess(string permission)
           {
             
               using (ServerApplicationContext ctx = ServerApplicationContext.Current)
               {
                   var currentUser = ctx.Application.User;
                   string s = System.Web.Security.Roles.ApplicationName;
                   RoleProvider provider = System.Web.Security.Roles.Provider;
                   return provider.IsUserInRole(currentUser.Identity.Name, permission);
               }
             
           }

    p.s. The AspNetRoleProvider in question ends up making a WCF call to another server too look up the permissions.

    • Edited by CostasZ Thursday, October 30, 2014 1:17 PM
    Thursday, October 30, 2014 1:15 PM
  • I solved the issue by creating a temporary class to hold the value across threads like this:

       

    public class boolClass    {        public bool isTrue;    }

                        
    private void GenericHasAccess(string permission, boolClass boolclass)
          {
              this.Details.Dispatcher.BeginInvoke(() =>
               {
    var currentUser = ServerApplicationContext.Current.Application.User;
                       string s = System.Web.Security.Roles.ApplicationName;
                       RoleProvider provider = System.Web.Security.Roles.Provider;
                       boolclass.isTrue = provider.IsUserInRole(currentUser.Identity.Name, permission);
               });
          }

    partial void MyEntity_CanUpdate(ref bool result)
          {
              boolClass bc = new boolClass();
              GenericHasAccess("MyEntity_CanUpdate", bc);
              result = bc.isTrue;
          }
    



    • Edited by CostasZ Tuesday, November 4, 2014 2:31 PM
    • Marked as answer by CostasZ Tuesday, November 4, 2014 2:32 PM
    Tuesday, November 4, 2014 2:31 PM