Concurrent Ria Service (and Web Api) requests in LightSwitch

Introduction

Recently I’ve developed a LightSwitch SilverLight web app with some expensive queries in a RIA Service.
In a certain screen, I had some filter criteria from a RIA Service.
The first filter was a rather expensive query (~2s) which I couldn’t easily optimize. The other filter criteria were cascading from that first filter and thus didn’t need any processing time (if filter1 is null returns null).

What I noticed is that all these RIA Service requests were executed at the same time, and also responded at the same time. All these filter drop down lists spinned for more then 2 seconds :S

Problem

If you have a screen in which certain RIA Service requests are executed consecutively, you may notice that one expensive query can hold up any following queries of a RiaService. This occurs in both the SL as the HTML5/Sharepoint client.

Example setup: 2 requests consecutively executed in 1 LightSwitch SL screen.

public class CustomerDomainService : LightSwitchDomainServiceBase
{
    [Query(IsDefault = true)]
    public IQueryable<CustomerDTO> GetDefault()
    {
        return null;
    }
    [Query(IsDefault = false)]
    public IQueryable<CustomerDTO> SlowRequest()
    {
        //emulate a slow request
        System.Threading.Thread.Sleep(2000);
        return from cust in Context.Customers
                select new CustomerDTO
                {
                    FirstName = cust.FirstName,
                    LastName = cust.LastName,
                    FullName = cust.FirstName + " " + cust.LastName
                };
    }

    [Query(IsDefault = false)]
    public IQueryable<CustomerDTO> FastRequest()
    {
        return from cust in Context.Customers
                select new CustomerDTO
                {
                    FirstName = cust.FirstName,
                    LastName = cust.LastName,
                    FullName = cust.FirstName + " " + cust.LastName
                };
    }
}

Both requests take +2s (IE9 – F12 Developer Tools)

image

This behavior doesn’t occur when working with the intrinsice LightSwitch dataservice, which is actually also a WCF Data Service. Strange…

Solution

Read about the ASP.NET Session State in MSDN, section Concurrent Requests and Session State.

This implies that if a client executes 3 consecutive requests, the server will process the requests serially because by default the session state is locked. Luckily, you can set the session state read-only so that this locking doesn’t occur but you still have access to the session state for any security implementations. This is how:

  1. Create a Global.asax file, if you don’t have one already.
  2. Set the sessionstatebehavior to readonly for any WCF Ria Service requests:
  3. protected void Application_BeginRequest(object sender, EventArgs e)
    {
        if (Context.Request.CurrentExecutionFilePath.EndsWith(".svc"))
        {
            Context.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.ReadOnly);
        }
    }
    

Now all requests to your WCF Ria Services will have a read-only sessionstate, and can happen in parallel :)

image

What about Web API Request?

This problem also occurs with Web API Requests, the solution can be easily applied by adding an attribute to your controller:

[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
public class ParallelController : Controller
{
    //...
}

Conclusion

Happy performance tuning! :)

About these ads

One thought on “Concurrent Ria Service (and Web Api) requests in LightSwitch

  1. Pingback: LightSwitch Community & Content Rollup- October 2013 - Beth Massi - Sharing the goodness - Site Home - MSDN Blogs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s