2

I have been asked to put a constraint that users should not be able to concurrently execute part of the code that calls C++ unmanaged code from ASP.Net/C# web application.

I wanted to ask if placing lock(someobject) around the C# code which is in a C# class library that calls the PInvoke/C++ code is sufficient to ensure that only one user will be able to execute the code? or is there another technique?

I might have to host this code as a WCF service. Would this approach would work too?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
InfoLearner
  • 14,952
  • 20
  • 76
  • 124

2 Answers2

5

If you use WCF you can adopt ConcurrencyMode.Single, which will automatically ensure that only one thread is performing an operation at any given moment within one context instance.

Else you would need to use manual thread syncronization (as lock, mutex etc.).

oleksii
  • 35,458
  • 16
  • 93
  • 163
  • Also, if using manual thread synchronization, your lock/mutex would need to be `static` so that it was shared by all requests. Otherwise, they'd each get their own copy of the lock and merrily execute concurrently. – GalacticCowboy Dec 14 '11 at 16:23
  • does it mean that on a WCF service, setting ConcurrencyMode.Single on a Per Call (Context Mode) service, multiple clients call to the WCF service will not be executed concurrently? – InfoLearner Dec 14 '11 at 17:17
  • @KnowledgeSeeker you right, instance context mode must be single. I updated my answer. – oleksii Dec 14 '11 at 17:23
  • if i use manual thread synchronization, do i still need to ensure that it's a single wcf service? or per call or per session would also work? – InfoLearner Dec 14 '11 at 23:34
  • Even if it's per call? because doesn't it mean that object will be created each time i call the service and same code will execute as the code will not be locked in the new instance? – InfoLearner Dec 15 '11 at 16:22
  • @KnowledgeSeeker I think I messed up. In PerCall instancing, concurrency is not relevant, because each message is processed by a new InstanceContext and, therefore, never more than one thread is active in the InstanceContext (from [docs](http://msdn.microsoft.com/en-us/library/ms731193.aspx)) and see [this answer](http://stackoverflow.com/a/6697622/706456). – oleksii Dec 15 '11 at 16:54
  • yep. that makes sense. so the conclusion is that we need a singleton service with locking mechanism to ensure only one application can execute the code. – InfoLearner Dec 16 '11 at 13:39
0

Placing a lock() on a variable while that code is executed is one way to go.

An article by Joe Albahari will probably give you some good advice on the best approach for your system.

There is also another SO post here: Can I add an attribute to a function to prevent reentry? that might help you as well.

In the past I've used [MethodImpl(MethodImplOptions.NoInlining)] (see MSDN) And it served it worked pretty well for synchronous actions.

Community
  • 1
  • 1
Ryan Ternier
  • 8,714
  • 4
  • 46
  • 69