Note that there are some explanatory texts on larger screens.

plurals
  1. POWCF data synchronization
    text
    copied!<p>Suppose I have a current bank account, and it automatically transfers money from my savings account when balance is low. So, I wrote below WCF code:</p> <pre><code>//Servicer side: [ServiceContract] public interface IBankAccount { [OperationContract] double withdraw(double amount); [OperationContract] double enquiry(); } class BankAccountService:IBankAccount { public double enquiry() { return balance; } public double withdraw(double amount) { while (balance &lt; amount) { transferMoreMoney(); } deduct(amount); return balance; } public void deduct(double amount) { System.Threading.Thread.Sleep(10000); balance -= amount; } public void transferMoreMoney() { System.Threading.Thread.Sleep(10000); balance += maximizeTransferAmount; } private static double balance; private double maximizeTransferAmount = 100.0; } //Client side: ServiceReference1.BankAccountClient client = new ServiceReference1.BankAccountClient(); while (true) { try { string tmpStr = Console.ReadLine(); if (tmpStr == "") break; double v0 = client.enquiry(); Console.WriteLine("Current balance is:{0}", v0); double v1 = Convert.ToDouble(tmpStr); Console.WriteLine("Amount withdrawn is:{0}", v1); double v2 = client.withdraw(v1); Console.WriteLine("Remaining balance is:{0}", v2); } catch (CommunicationException e) { Console.WriteLine(e.Message); } } </code></pre> <p>The question is, when I have multiple clients calling the same service, the balance can be negative. How can I ensure that the balance would be replenished on time without ending up negative?</p> <p>Also, I still have other clients that are only running balance queries, so if they only query, they should not be kept waiting, who to ensure this?</p> <p>This is only an example illustrating what I need. This example illustrate the technical issue I need to address, but not the real case. I can't use database, because my real case is one that requires high-performance real-time calculation in memory, so database is not an option. </p> <p>More basically, is there something that resembles "lock" in WCF service, when multiple clients call the same service that shares the same data?</p> <p>Thank you very much.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload