I came across this issue when 2 different users of my website created a product sale at the same time. This should generate 2 different requests to the server and print 2 different sale tickets. The problem is that when the server requests come in at the same time (or very close from each other), both branches print the same tickets, instead of each printing its own ticket.
Here's my code:
Branch Total CreatedAt ProductSaleId
EBQ Centro Maya 35.00 2016-06-13 15:35:54.743 3263825d-bca3-4d18-bbca-3eebe4c3398a
EBQ PA Chetumal 30.00 2016-06-13 15:35:54.647 01f52d7d-5745-426b-a973-4a701a18b8e4
These are the DB entries, 2 different branches made a sale each at 15:35:54.
So, once the sale is made, PrintReceipt method is called like this:
HelperObjects.ThermalPOS58.PrinterController.PrintReceipt(new InvoiceReceipt()
{
Discount = discount,
InvoiceNumber = "TEST",
SubTotal = decimal.Parse(createProductSale.Total),
Total = total,
Change = createProductSale.Change ?? "0",
ClientAmount = createProductSale.ClientAmount,
InvoiceDateFormatted =
DateTime.UtcNow.ConvertUtcTimeToTimeZone("", User.Identity.GetUserId())
.ToString("dd/MM/yyyy HH:mm:ss"),
BranchName = _unitOfWork.BranchRepository.GetById(branchId).Name,
InvoiceItems = invoiceReceipts,
PrinterName = _unitOfWork.BranchRepository.GetById(branchId).PrinterName,
IsTpv = createProductSale.IsTpv
});
PrintReceipt method looks like this:
private static InvoiceReceipt _mappedInvoice ;
public static void PrintReceipt(InvoiceReceipt invoiceReceipt)
{
_mappedInvoice = invoiceReceipt;
var printNodeIntegration = new PrintNodeIntegration();
printNodeIntegration.Print(GetDocument(), invoiceReceipt.PrinterName);
}
Each call is sending its own invoice to the PrintReceipt method and it works fine EXCEPT when the sales are made at the same time. When this happens, both branches are printing the same receipt. Usually the one that came in first.
Any ideas?
static
members are shared within the process. In ASP.NET all sessions in an app pool use the same process, so they all share the same static
state.
The static Session
property separates data by user session, so it is a better place to hold session-specific data:
Session["mappedInvoice"] = invoiceReceipt;
But it's unclear how you're using it to know if it even needs to be stored in the session. Typically You use the session to hold data that needs to persist across requests.