Hi,
Given an exe that contains multiple services, with LoaderOptimization of
SingleDomain, I would presume that each service would be created in
separate AppDomain, and they would access to separate Singletons. 
But this is not the case!
Below is code that shows that they access the *same* Singleton.
After starting both services, the event log shows one entry for
"Singleton created" & two entries for "Method called"
(Singleton from
http://msdn.microsoft.com/library/en-us/dnbda/html/singletondespatt.asp?
frame=true)

namespace MyNamespace
{
        sealed class Singleton
        {
                private EventLog myLog;
                private Singleton()
                {
                        myLog = new EventLog();
                        myLog.Source = "SingletonTester";
                        myLog.WriteEntry("Singleton
created",EventLogEntryType.Information);
                }
                public static readonly Singleton Instance = new
Singleton();
                public void Method()
                {
                        myLog.WriteEntry("Method
called",EventLogEntryType.Information);
                }
        }
        public class MyService : System.ServiceProcess.ServiceBase
        {
                public MyService(string param)
                {
                        this.ServiceName = "MyService" + param;
                }
                [LoaderOptimization(LoaderOptimization.SingleDomain)]
                static void Main(string[] args)
                {
                        System.ServiceProcess.ServiceBase[]
ServicesToRun;
                        ServicesToRun = new
System.ServiceProcess.ServiceBase[] { new MyService("1"), new
MyService("2") };
        
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
                }
                protected override void OnStart(string[] args)
                {
                        Singleton.Instance.Method();
                }
        }
}
[RunInstallerAttribute(true)]
public class ProjectInstaller: Installer
{
        private ServiceInstaller serviceInstallerLive;
        private ServiceInstaller serviceInstallerTest;
        private ServiceProcessInstaller processInstaller;

        public ProjectInstaller()
        {
                serviceInstallerLive = new ServiceInstaller();
                serviceInstallerLive.StartType =
ServiceStartMode.Manual;
                serviceInstallerLive.ServiceName = "MyService1";
                serviceInstallerLive.DisplayName = "MyService1";
                Installers.Add(serviceInstallerLive);

                serviceInstallerTest = new ServiceInstaller();
                serviceInstallerTest.StartType =
ServiceStartMode.Manual;
                serviceInstallerTest.ServiceName = "MyService2";
                serviceInstallerTest.DisplayName = "MyService2";
                Installers.Add(serviceInstallerTest);

                processInstaller = new ServiceProcessInstaller();
                processInstaller.Account = ServiceAccount.LocalSystem;
                Installers.Add(processInstaller);
        }
}
-------Original Message
>Date:    Mon, 3 Jun 2002 17:09:04 -0600
>From:    Greg Reinacker <[EMAIL PROTECTED]>
>Subject: Re: Singleton pattern
>
>> Don't you automatically get one
>> instance of a static class in each app-domain?
>
>This behavior depends on the LoaderOptimization setting, which is set
>when an AppDomain is created (either at AppDomain.CreateDomain, or
>CorBindToRuntimeHost).
>
>Greg Reinacker
>Reinacker & Associates, Inc.
>http://www.rassoc.com
>

You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced 
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to