caloggins
11/7/2013 - 3:49 PM

A main method

A main method

    class Program
    {
        static void Main()
        {
            try
            {
                var container = WindsorFactory.Create();

                RunTheHostFactory(container);
            }
            catch (Exception exception)
            {
                var assemblyName = typeof(Program).AssemblyQualifiedName;

                if (!EventLog.SourceExists(assemblyName))
                    EventLog.CreateEventSource(assemblyName, "Application");

                var log = new EventLog { Source = assemblyName };
                log.WriteEntry(string.Format("{0}", exception), EventLogEntryType.Error);
            }
        }

        private static void RunTheHostFactory(IWindsorContainer container)
        {
            HostFactory.Run(config =>
            {
                config.Service<IIrisMonitorService>(settings =>
                {
                    // use this to instantiate the service
                    settings.ConstructUsing(hostSettings => container.Resolve<IIrisMonitorService>());
                    settings.WhenStarted(service => service.Start());
                    settings.WhenStopped(service =>
                    {
                        service.Stop();
                        container.Release(service);
                        container.Dispose();
                    });
                });

                config.UseNLog();

                config.RunAsLocalSystem();

                var description = container.Resolve<ServiceDescription>();
                config.SetDescription(description.Description);
                config.SetDisplayName(description.DisplayName);
                config.SetServiceName(description.ServiceName);
            });
        }
    }