Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF InstanceContextMode: Per Call vs. Single in this scenario

Tags:

wcf

I want to avoid generating duplicate numbers in my system.

CreateNextNumber() will:

  1. Find the last number created.
  2. Increment the value by one.
  3. Update the value in the database with the new number.

I want to avoid two clients calling this method at the same time. My fear is they will pull the same last number created, increment it by one, and return the duplicate number for both clients.

Questions:

  1. Do I need to use single mode here? I'd rather use Per Call if possible.
  2. The default concurrency mode is single. I don't understand how Per Call would create multiple instances, yet have a single thread. Does that mean that even though multiple instances are created, only one client at a time can call a method in their instance?
like image 412
Mike Avatar asked Dec 21 '25 05:12

Mike


1 Answers

If you use InstanceContextMode.Single and ConcurrentcyMode.Single your service will handle one request at a time and so would give you this feature - however, this issue is better handled in the database

Couple of options:

  1. make the field that requires the unique number an identity column and the database will ensure no duplicate values
  2. Wrap the incrementing of the control value in a stored procedure that uses isolation level RepeatableRead and read, increment and write in a transaction

for your questions you might find my blog article on instancing and concurrency useful

like image 159
Richard Blewett Avatar answered Dec 24 '25 04:12

Richard Blewett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!