How to avoid blocking requests?

Asked

Viewed 278 times

2

I’m doing a project in ASP.NET MVC + IIS and realized that when making a request to a page with long reading in the Database (using Entity Framework), if I open another window and try to access any other page of the site (the Home for example, which has no reading and should open quickly), this second request ends up waiting for the first to finish to then display the page.

I’m worried about this type of blocking, because when the site is in production it may end up blocking the request of customers and even generating a timeout.

I don’t know if this is some kind of configuration that should be inserted into the IIS or the application.

If anyone can help me with the following question, thank you.

PS: I’m not using asynchronous methods in Controllers. I believe that the asynchronous method would only avoid freezing the screen for the user, but it does not interfere with blocking the requests of other clients, but as I do not have much knowledge in asynchronous programming, if I am wrong correct me.

1 answer

3


I don’t know if this is some kind of configuration that should be inserted into the IIS or the application.

Have you answered yourself in this comment here:

I’m not using asynchronous methods in the controllers. I believe that the asynchronous method would only avoid freezing the screen for the user, but it does not interfere with blocking the requests of other clients, but as I do not have much knowledge in asynchronous programming, if I am wrong correct me.

Yes, blocking occurs because the context is not prepared for thread-Safety, then all operations are aligned in synchronous mode.

Already in asynchronous mode, the Controller performs all the instructions of Action that do not require the information of the bank, until arriving at the execution of some interaction with the context. For example:

var produtos = await db.Produtos.ToListAsync();

In this case, a thread at a high level (implemented by Task<T>) will be created to run the database. With this, the Action does not block the execution of other Actions contestants.

How to avoid blocking requests?

In short:

  1. Update your solution to at least . NET 4.5.2;
  2. Use asynchronous scope in your Actions:

    public async Task<ActionResult> MinhaAction() { ... }
    
  3. When calling context, envelop the information request using an asynchronous method (System.Data.Entity) and prepare C# to uncouple the request result using await:

    var produtos = await db.Produtos.ToListAsync();
    
  4. When using transactional scope, tell the transaction coordinator that you are using an asynchronous flow:

    using System.Transactions;
    
    using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) 
    {
        ...
    
        scope.Complete();
    }
    
  • 1

    Perfect guy, I think this question will help a lot of people too. I thank you for the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.