locked
The current user does not have permission to execute the '<EntityName>_Single' query.

    Question

  •  

    In my custom code I am trying to call the RIA services directly

     

    this.DataWorkspace.MyData.SomeEntity_Single(id);

     

    I am getting an exception PermissionException

     

    The current user does not have permission to execute the '<EntityName>_Single' query.

     

    Why I am getting this exception? Is is not supposed to call directly the DataWorkspace?

     

    Thanks in adavance

    Wednesday, August 25, 2010 2:46 PM

Answers

  • - In my screen, on partial method BeforeDataInitialize I subscribe an event of the custom control with my handler

    - In my handler code I call the method this.DataWorkspace.MyData.SomeEntity_Single(id) getting the Permission exceptio

    The issue lies in the threading architecture of LightSwitch.  On the client, each screen gets 2 dispatchers/threads.  The Main UI dispatcher that all screens share and a background "Logic" dispatcher that is owned by that screen.  When you write code in a partial method, that code is executed on the background "Logic" dispatcher/thread.  That way, if you execute any long running operations (a query, a save, etc.) the UI thread isn't blocked and the app stays responsive.

    The problem with your app is that your custom control raises its event on the Main UI Dispatcher.  However, we expect all code to be executed on the Logic Dispatcher, so the app stays responsive.  What you want to do is wrap your event handler with a call to BeginInvoke on the logic dispatcher:

     

     

    this.Details.Dispatcher.BeginInvoke(() =>

    {

     

     

       // Write your code here.

    });

    That way you won't block the Main UI thread, and the permission check will happen successfully.

    Wednesday, August 25, 2010 4:28 PM

All replies

  • In my custom code I am trying to call the RIA services directly

    this.DataWorkspace.MyData.SomeEntity_Single(id);

    I am getting an exception PermissionException

    The current user does not have permission to execute the '<EntityName>_Single' query.

    From outside of LS, you're trying to call the RIA endpoint directly?  Is that right?

    In that case, tHe message you are getting is correct - you won't have permission.  You must first authenticate to the LS middle tier as a valid user of that app before _any_ of the middle tier endpoints will service your requests.

    What kind of auth is your LS app using?  Windows or Forms?

     

    Wednesday, August 25, 2010 3:12 PM

  • My code is inside LS. The code is in the partial method BeforeDataInitialize on that screen. My App is not using Authentication at all.

     

    It is atrange behaviour.

     

    Wednesday, August 25, 2010 3:35 PM
  • I cannot reproduce this behavior using a Beta1 build.  Can you provide more information?  I think I would maybe need to look at the whole project.

    Did you implement any "Can" methods?  Like <EntityName>_CanRead, or <EntityName_Single_CanExecute?  If a "Can" method returns 'false', LightSwitch will throw a PermissionException when that operation is attempted.

    Wednesday, August 25, 2010 4:04 PM

  • I did not implement any "Can" methods.

     

    Here is the scenario:

    - 3 tier web

    - No authentication

    - In my screen I have a custom control which fire some events

    - In my screen, on partial method BeforeDataInitialize I subscribe an event of the custom control with my handler

    - In my handler code I call the method this.DataWorkspace.MyData.SomeEntity_Single(id) getting the Permission exception

    -

     

    Wednesday, August 25, 2010 4:10 PM
  • - In my screen, on partial method BeforeDataInitialize I subscribe an event of the custom control with my handler

    - In my handler code I call the method this.DataWorkspace.MyData.SomeEntity_Single(id) getting the Permission exceptio

    The issue lies in the threading architecture of LightSwitch.  On the client, each screen gets 2 dispatchers/threads.  The Main UI dispatcher that all screens share and a background "Logic" dispatcher that is owned by that screen.  When you write code in a partial method, that code is executed on the background "Logic" dispatcher/thread.  That way, if you execute any long running operations (a query, a save, etc.) the UI thread isn't blocked and the app stays responsive.

    The problem with your app is that your custom control raises its event on the Main UI Dispatcher.  However, we expect all code to be executed on the Logic Dispatcher, so the app stays responsive.  What you want to do is wrap your event handler with a call to BeginInvoke on the logic dispatcher:

     

     

    this.Details.Dispatcher.BeginInvoke(() =>

    {

     

     

       // Write your code here.

    });

    That way you won't block the Main UI thread, and the permission check will happen successfully.

    Wednesday, August 25, 2010 4:28 PM
  • Thanks Eric.

     

    When calling the RIA service in the Logic dispatcher, it works!

     

    However, I expected that, if I set some properties on controls getted via FindControl (visibility, etc.) on Logic dispatcher, will receive an exception. It seems that in the Logic thread I can manipulate controls.

     

    Thanks

    Wednesday, August 25, 2010 5:00 PM
  • When you use our built-in LightSwitch controls, we handle going between the dispatchers correctly.  So yes, you can get and set control properties from the Logic dispatcher.  Notice that the type you get back from IScreenObject.FindControl is an IContentItemProxy.  However, if you want to get and set properties on your custom control, you will probably need to execute that code on the Main UI Dispatcher, which can be accomplished with:

    Microsoft.LightSwitch.Threading.

     

    Dispatchers.Main.BeginInvoke(() =>

    {

     

     

    // Write your code here.

    });

     

     

    Wednesday, August 25, 2010 5:33 PM
  • Hi Eric,

    I had the same problem accessing a DataWorkspace _Single query from screen code as well. Your answer allowed me to successfully run the code, so thanks for that!

    I was halfway there, lol, I was trying to use Me.Dispatchers.Main.BeginInvoke(), like I needed to do to set up property changed notification on the screen's entity property.

    I didn't know there were TWO threads per screen, so I've learned something new, in addition to getting my code to run.

    Thanks again,

    Yann
    Thursday, March 10, 2011 6:43 AM