7

Can't udnerstand why type constructor for PerSession/ WCF service was calling twice. ConcurrencyMode is Multiple. Just launching five simultaneous clients which do the same WCF service method call, in a log I see that static constructor was called twice, the first time and after 3 seconds second time with an other ProcessId/ThreadId. No exceptions neither in constructor itself nor WCF trace logs. Constructor execution time is ~10 milliseconds as per log. This results in all static fields are not shared between all service instances as supposed and in case of 5 client connections I have 5 services and two different static contexts so change in onse static field is not reflected for all services.

This issue confuses many things since I am relying on some static caches shared across multiple service instances.

Service is hosted in IIS. No IIS restarts, AppPool recycles on this time interval.

[AspNetCompatibilityRequirements(RequirementsMode =
  AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(
  InstanceContextMode = InstanceContextMode.PerSession, 
  IncludeExceptionDetailInFaults = true, 
  ConcurrencyMode = ConcurrencyMode.Multiple)]
public class WcfService
{ 
    private static readonly ILog logger;
    private static volatile bool typeInitialized;

    static WcfService()
    {
        try
        {
            // Here is typeInitialized is false in both calls
            logger = LogManager.GetLogger("LogName");

            logger.InfoFormat("[PID:{0}] [THID:{1}] BEGIN Static constructor",
               Process.GetCurrentProcess().Id,
               Thread.CurrentThread.ManagedThreadId);
        }
        catch (Exception exception)
        {
           logger.Error("error on type construction stage", exception);
        }
        finally
        {
            logger.InfoFormat("[PID:{0}] [THID:{1}] END Static constructor",
               Process.GetCurrentProcess().Id,
               Thread.CurrentThread.ManagedThreadId);               
           typeInitialized = true;
        }
    }
}
sll
  • 61,540
  • 22
  • 104
  • 156
  • If your goal is to ensure only one instance is created you may want to implement Singleton pattern for this class: http://en.wikipedia.org/wiki/Singleton_pattern – Nogard Dec 25 '12 at 14:19
  • 2
    @Nogard and that will be SO totally useless in this case as IIS is opening multiple instances of the program, which all are happy to have their own singleton. Static constructors are also DEFINED as being called ONLY ONCE ON CLASS LOAD - but that does not stop the user from having separate appdomains that load the classes separately, as is the case with IIS. – TomTom Dec 25 '12 at 14:28

1 Answers1

6

Assuming you're hosting your service with IIS, this is normal behaviour unless you explicitly configure the process to only permit a single AppDomain from being spun up.

If you look at the list of running processes, you'll find each process Id in your log corresponds with a copy of w3wp.exe hosting a separate appdomain.

Bevan
  • 43,618
  • 10
  • 81
  • 133
  • You are right, this is IIS hosted service. Forgot mentioning this. Just updated question. Can I setup single `AppDomain` behaviour only for a particular WCF service? Because around are other services as well so I do not want mess up anything other – sll Dec 25 '12 at 14:33
  • I had 2 worker processes per AppPool, after changing to `1` everythign working fine so far, anyway appreciate any advice regarding explicitly specifying to host all WCf instances in the same process – sll Dec 25 '12 at 14:48