How to avoid request blocking?

2

I'm doing a project in ASP.NET MVC + IIS and I realized that when I made a request to a page with a long read in the database Entity Framework ), if I open another window and try to access another page of the site (the Home for example, which has no read and should open quickly), this second request ends waiting for the first one to finish and then display the page.

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

I do not know if this is some kind of configuration that should be entered in IIS or in 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 prevent the screen freeze for the user, but it does not interfere with the blocking of requests from other clients, but as I am not very knowledgeable in asynchronous programming, if I am wrong, correct me. >     

asked by anonymous 21.04.2017 / 17:36

1 answer

2
  

I do not know if this is some kind of configuration that should be entered in IIS or in the application.

You have already responded to this comment here:

  

I'm not using asynchronous methods in Controllers. I believe that the asynchronous method would only prevent the screen freeze for the user, but it does not interfere with the blocking of requests from other clients, but as I am not very knowledgeable in asynchronous programming, if I am wrong correct me.

Yes, the locking occurs because the context is not prepared for thread-safety , so all operations are queued in synchronous mode.

In asynchronous mode, Controller executes all Action statements that do not require the bank's information, until it performs some interaction with the context. For example:

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

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

  

How to avoid request blocking?

In summary:

  • Upgrade your solution to at least .NET 4.5.2;
  • Use asynchronous scope in your Actions :

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

    var produtos = await db.Produtos.ToListAsync();
    
  • 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();
    }
    
  • 21.04.2017 / 18:16