2

In a WCF being run from IIS I have this code (well something similar).

public static class Test
{
   public static int Method(int x, int y)
   {
      int p = 10;
      int r = p * x * y;
      return r;
    }
}

This method can then in theory be called from 1 million requests from 1 million different users simultaneously.

I have an argument discussion with a colleague where my stand point is that each instance of this method call is separate and will never cause data corruption between calls. This is supported by this thread: C# : What if a static method is called from multiple threads?

My colleague however claims that IIS and WCF are a special kind of monster that can cache and start/stop calls in all kinds of weird places so that in practice these calls actually can interfer with one another.

The example he made was that if one call is inside the method and another one comes in with new paramaters then the int x and int y paramater variables values could potentially be replaced for the already ongoing call.

His stand point is NEVER use static methods / classes in anything IIS / WCF related.

I haven't been able to find any solid documentation that supports either case, anyone care to throw some well documentet arguments at this?

Community
  • 1
  • 1
JensB
  • 6,663
  • 2
  • 55
  • 94
  • possibly they are confusing the question of thread safety with the question of whether the singleton will be the only instance used by your website. I think they are right in the general rule about not using the singleton pattern in websites. even if everything else works ok, consider a web farm – Ewan Mar 13 '15 at 12:03

1 Answers1

3

This is not the case.

.NET code does not have the ability to interfere with other code's execution. For example it cannot cache calls or stop them or cause local variables to be shared when they are not. The CLI specification clearly prescribes the way static methods are executed.

Even if the authors of the WCF framework wanted to do such a thing they could not. (At least not without CLR runtime support.)

If it suddenly was no longer safe to call static methods (!) what piece of code could still run?! You couldn't even call string.Join.

WCF does no such thing. Your colleague needs to provide some evidence now for what he has claimed. Such outrageous claims often arise from a confusing experience made by that person. Maybe he fixed a bug by making a method non-static and doing something else without realizing it. Now he beliefs that the static property was the cause for the bug. This can lead to superstitious beliefs.

usr
  • 168,620
  • 35
  • 240
  • 369