Competition Server ASP.NET

Asked

Viewed 203 times

3

I have a Silverlight application, which accesses a service in WCF. In this application I have a method Salvarnotafiscal(). This method calls a method Validarnotafiscal that verifies if there is already a tax note with the number and series informed. This Salvarnotafiscal method is quite large, because it performs several routines.

It occurred to the client to click this button several times, and 6 notes with the same number were launched in the system; the scenario was this believe, because there would be no other way to duplicate these notes if there is validation.

I believe that ASP.NET meets the requests in parallel, which makes sense to me, because there may be multiple simultaneous users and the server needs to meet all at the same time.

As for my Silverlight problem, I have tried to disable the button when it is clicked and enable callback return, but it has not worked.

public void Salvar()
{
  btnSalvar.IsEnabled = false; 
  NotaFiscalClient objSvcNotaFiscal = new NotaFiscalClient();
  objSvcNotaFiscal.SalvarNotaFiscalAsync(this.objNotaFiscal);
  objSvcNotaFiscal.SalvarNotaFiscalCompleted += (s, e) =>
       btnSalvar.IsEnabled = true;         
  };
}
  • What code within Salvarnotafiscalasync() does it place there

  • The code is gigantic, calls multiple methods, integrates with webservices, and I can’t post by company privacy rules. This block above I can still. Validation works normally in a normal scenario. The problem is user who keeps clicking the button several times. Even the screen is frozen, but until it freezes it gives time for the way to send several similar requests.

  • So, all you have to do is put up a traffic light that I think will solve your problem

  • What would that be in practice?

  • 1

    At a glance, this will block another insertion WHILE another is not over. http://www.bufaloinfo.com.br/ExibeNoticias.aspx?entryid=7326980343120390112

  • 5

    You can do this with Transactionscope http://answall.com/questions/32299/controle-de-concorr%C3%Aancia-em-inser%C3%A7%C3%A3o-no-banco-de-dados

  • That’s my question. Maybe I should close the post

Show 2 more comments

1 answer

0

Yes, HTTP requests occur in parallel.

It is possible to block this from happening in several ways...

First start by blocking the user from pressing the button several times before a request is completed... (just put a simple LOCK in the method Save which must already be resolved!)

Regarding multiple users generating the note with the same number, you will have to do the treatment in the back end (WCF), the easiest is also through the instruction LOCK, but only if you have a single server running a single WCF process will it work!

If the webservice is running through multiple concurrent processes, you can use a Mutex to ensure one-off access to an excerpt of the code (or the entire method, in its case)

Remembering that both the LOCK statement and the use of the Mutex class, you need to create a logic that only allows one note at a time to enter the process according to the note number and not generalized, if you do not have a row of notes to process, being one at a time, slowing down users.

If your back-end (WCF) is distributed (on multiple servers, load balancing, failover, etc.) you will not succeed with just a LOCK/MUTEX, you will need to create a system where there will be a single Locks control server and then lock there, or better, you can do this using a database (SQL) or even Redis. In a better solution for this scenario would be to implement a messaging system (MSMQ or Redis) where only one host would be responsible for processing the notes, and the others just performing the other tasks...

Browser other questions tagged

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