c# – ASP.NET Server Competition

Question:

I have a Silverlight application, which accesses a WCF service. In this application I have a SaveInvoice() method. This method calls a ValidarInvoice method that checks if there is already an invoice with the informed number and series. This TaxSave Invoice method is quite big, as it runs several routines.

The customer clicked on this button several times, and 6 notes with the same number were posted in the system; the scenario was that I believe, as there would be no other way to duplicate these notes if there is validation.

I believe ASP.NET serves requests in parallel, which makes sense to me as there can be multiple simultaneous users and the server needs to service them all at the same time.

As for my problem in Silverlight, I've tried disabling the button when it's clicked and enabling it on callback, but it hasn't worked.

public void Salvar()
{
  btnSalvar.IsEnabled = false; 
  NotaFiscalClient objSvcNotaFiscal = new NotaFiscalClient();
  objSvcNotaFiscal.SalvarNotaFiscalAsync(this.objNotaFiscal);
  objSvcNotaFiscal.SalvarNotaFiscalCompleted += (s, e) =>
       btnSalvar.IsEnabled = true;         
  };
}

Answer:

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 multiple times, before a request is completed… (just put a simple LOCK on the Save method which should already resolve!)

In relation to 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 LOCK statement, but only if you have a single server running a single process. WCF will work!

If the webservice is running through several simultaneous processes, you can use a Mutex to guarantee single access to a piece of code (or the entire method, in your case)

Remembering that both the LOCK instruction 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, otherwise you will have a row of notes to process, one at a time, slowing down users.

If your back-end (WCF) is distributed (on several servers, load balancing, failover, etc) you will not be successful with just one LOCK/MUTEX, you will need to create a system in which there will be a single server of lock control and then lock there, or better, you can do it using a database (SQL) or even Redis. 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 would just perform the other tasks…

Scroll to Top