Click here to Skip to main content
15,886,873 members
Articles / Desktop Programming / Win32

IP Watchdog: Simple Windows Service Written in C#

Rate me:
Please Sign up or sign in to vote.
4.71/5 (50 votes)
14 Aug 2012CPOL9 min read 115.3K   5.3K   170  
Self-installing windows service that monitors computer's WAN address and sends e-mail when it changes
using IpWatchDog.Install;
using IpWatchDog.Log;
using IpWatchDog.Runners;

namespace IpWatchDog
{
    class Configurator
    {
        private ILog _log;
        private bool _isConsole;

        public Configurator(bool isConsole)
        {
            _isConsole = isConsole;
            _log = 
                isConsole ?
                (ILog)new ConsoleLog() :
                new SystemLog(StringConstants.ServiceName);
        }

        public IRunner CreateRunner()
        {
            var service = CreateWatchDogService();
            return _isConsole ?
                (IRunner)new ConsoleRunner(service) :
                new ServiceRunner(service, StringConstants.ServiceName);
        }

        public InstallUtil CreateInstaller()
        {
            return new InstallUtil(_log);
        }

        public IService CreateServiceController()
        {
            return new ServiceController(_log);
        }

        private IService CreateWatchDogService()
        {
            var config = new AppConfig();
            var notifier = CreateNotifier(config);

            return new IpWatchDogService(
                _log, 
                config,
                new IpPersistor(_log), 
                new WebIpRetriever(_log), 
                notifier);
        }

        private IIpNotifier CreateNotifier(AppConfig config)
        {
            if (string.IsNullOrEmpty(config.Command))
            {
                return new MailIpNotifier(_log, config);
            }
            else
            {
                return new CommandIpNotifier(_log, config);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Thomson Reuters
United States United States
Ivan is a hands-on software architect/technical lead working for Thomson Reuters in the New York City area. At present I am mostly building complex multi-threaded WPF application for the financial sector, but I am also interested in cloud computing, web development, mobile development, etc.

Please visit my web site: www.ikriv.com.

Comments and Discussions