Michael
Michael

Reputation: 712

WCF data synchronization

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:

//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 < 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);
            }
        }

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?

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?

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.

More basically, is there something that resembles "lock" in WCF service, when multiple clients call the same service that shares the same data?

Thank you very much.

Upvotes: 1

Views: 1086

Answers (2)

Brijesh Mishra
Brijesh Mishra

Reputation: 2748

Actually you must be using ConcurrencyMode.Single, concurentcy mode single will queue all call to service BankAccountService and request will be executed one after other. If you go for ConcurrencyMode.Multiple you yourself have to implement thread lock.

Upvotes: 3

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28980

you must define behavior specific for your service wcf ( Singleton Instance Mode +  Concurrency Mode Multiple)

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
        ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class BankAccountService: IBankAccount
    {

    }

Note : You can also define your behavior in config file

Upvotes: 3

Related Questions