Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Deployer

, 30 Mar 2012
Automate deployment of Windows Services, ClickOnce, and other .NET applications.
deployer_src.zip
Deployer
Deployer
!copyToDllCache
Interop.VSFlex7L.dll
stdole.dll
ClassDiagram1.cd
clickOnceKey.pfx
deployer-full.xsx
Deployer.csproj.user
DeployTypes
DomainClasses
_tfs
text-base
ConfigReplacesEntry.cs.tfs-base
ConfigReplacesRoot.cs.tfs-base
DllCacheEntry.cs.tfs-base
_tfs
text-base
BaseDeployType.cs.tfs-base
ClickOnce.cs.tfs-base
DatabaseScript.cs.tfs-base
Service.cs.tfs-base
XCopy.cs.tfs-base
Logic
_tfs
text-base
Controller.cs.tfs-base
FileSystemUtil.cs.tfs-base
XmlUtil.cs.tfs-base
mage.exe
Properties
Settings.settings
_tfs
text-base
AssemblyInfo.cs.tfs-base
Resources.Designer.cs.tfs-base
Resources.resx.tfs-base
Settings.Designer.cs.tfs-base
Settings.settings.tfs-base
Zip
.svn
entries
format
prop-base
_tfs
text-base
props
_tfs
text-base
text-base
ZipArchive.cs.svn-base
ZipDirectory.cs.svn-base
ZipEntry.cs.svn-base
ZipEntryCollection.cs.svn-base
ZipException.cs.svn-base
ZipLib.cs.svn-base
ZipStream.cs.svn-base
_tfs
text-base
ZipArchive.cs.svn-base.tfs-base
ZipDirectory.cs.svn-base.tfs-base
ZipEntry.cs.svn-base.tfs-base
ZipEntryCollection.cs.svn-base.tfs-base
ZipException.cs.svn-base.tfs-base
ZipLib.cs.svn-base.tfs-base
ZipStream.cs.svn-base.tfs-base
tmp
prop-base
_tfs
text-base
props
_tfs
text-base
text-base
_tfs
text-base
_tfs
text-base
_tfs
text-base
entries.tfs-base
format.tfs-base
_tfs
text-base
ZipArchive.cs.tfs-base
ZipDirectory.cs.tfs-base
ZipEntry.cs.tfs-base
ZipEntryCollection.cs.tfs-base
ZipException.cs.tfs-base
ZipLib.cs.tfs-base
ZipStream.cs.tfs-base
zlib.dll
TestApp
Properties
Settings.settings
_tfs
text-base
AssemblyInfo.cs.tfs-base
Resources.Designer.cs.tfs-base
Resources.resx.tfs-base
Settings.Designer.cs.tfs-base
Settings.settings.tfs-base
TestClickOnceApp
ClickOnceApp_TemporaryKey.pfx
Properties
Settings.settings
_tfs
text-base
AssemblyInfo.cs.tfs-base
Resources.Designer.cs.tfs-base
Resources.resx.tfs-base
Settings.Designer.cs.tfs-base
Settings.settings.tfs-base
TestClickOnceApp.csproj.user
TestDatabaseScript
Properties
_tfs
text-base
AssemblyInfo.cs.tfs-base
TestWindowsService
Properties
_tfs
text-base
AssemblyInfo.cs.tfs-base
someFiles
_tfs
text-base
will not be included in backup.txt.tfs-base
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration.Install;
using System.ServiceProcess;
using System.Xml;
using System.IO;
using System.Threading;
using Deployer.Logic;

namespace Deployer.DeployTypes
{
    class Service : XCopy
    {
        public Service(XmlNode configNode) : base(configNode)
        {
            _serviceName = configNode.SelectSingleNode("serviceName").InnerXml;
            _serviceMachine = XmlUtil.ParseStringNode(configNode.SelectSingleNode("serviceMachine"), ".");

            // find service
            foreach (ServiceController sc in ServiceController.GetServices(_serviceMachine))
            {
                if (sc.ServiceName == _serviceName)
                    _serviceController = sc;
            }
        }

        private ServiceController _serviceController;
        private string _serviceName;
        private string _serviceMachine;
        private bool LocalMachineService
        {
            get { return _serviceMachine == "." || _serviceMachine.ToLower() == Environment.MachineName.ToLower(); }
        }

        public override void Execute(System.IO.DirectoryInfo sourceDirectory)
        {
            // if service was found during initialization stop it
			if (_serviceController != null && _serviceController.CanStop)
			{
				_serviceController.Stop();
				_serviceController.WaitForStatus(ServiceControllerStatus.Stopped);

				// it seems that wait for status doesn't work properly when stopping service on remote machine
				// in any case to make sure that it is stopped we wait a bit
				Thread.Sleep(5000);
			}
			else
			{
				if (_serviceController != null)
				{
					onDeployNotice("Can't stop service '{0}'.", _serviceName);
				}
				else
				{
                    onDeployNotice("Service '{0}' does not exist.", _serviceName);
				}
			}

            base.Execute(sourceDirectory);

            // if service was not found install it
            if (_serviceController == null && LocalMachineService)
            {
                onDeployNotice("Instaling service '{0}'.", _serviceName);

                ManagedInstallerClass.InstallHelper(new string[] { "/i", FindServiceExe(TargetDirectory).FullName });
                _serviceController = new ServiceController(_serviceName);
            }
            else if (_serviceController == null && !LocalMachineService)
            {
                onDeployNotice("Can't install and start service '{0}', on remote machine '{1}'. You need to register service by hand :(", _serviceName, _serviceMachine);
            }
            // TODO: if service was found but target path is different, reinstall it


            if (_serviceController != null)
                _serviceController.Start();
        }

        private FileInfo FindServiceExe(DirectoryInfo directory)
        {
            foreach (FileInfo fi in directory.GetFiles("*.exe"))
            {
                // return first exe file
                if (fi.Name.ToLower().IndexOf(".vshost.") == -1)
                    return fi;
            }

            throw new Exception("Could not find exe file... Service deploy must have .exe file");
        }
    }
}

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 Apache License, Version 2.0

Share

About the Author

Predrag Tomasevic
Chief Technology Officer Atama Group
United States United States
http://www.linkedin.com/in/ptomasevic
 
Bitcoin wallet: 1ARXmh58S6aRiZPCbneDCzrB1HUU6PLmDQ

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 30 Mar 2012
Article Copyright 2008 by Predrag Tomasevic
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid