Tests/TestHelpers/DSCTestService.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
namespace TestServiceNamespace
{
    using System;
    using System.ComponentModel;
    using System.Configuration.Install;
    using System.IO;
    using System.ServiceProcess;

    [RunInstaller(true)]
    public class MyProjectInstaller : Installer
    {
        private ServiceInstaller serviceInstaller;
        private ServiceProcessInstaller processInstaller;

        public MyProjectInstaller()
        {
            processInstaller = new ServiceProcessInstaller();
            serviceInstaller = new ServiceInstaller();
            processInstaller.Account = ServiceAccount.LocalSystem;
            serviceInstaller.StartType = ServiceStartMode.Manual;
            serviceInstaller.ServiceName = TestService.TestServiceName;
            serviceInstaller.DisplayName = TestService.TestServiceDisplayName;
            serviceInstaller.Description = TestService.TestServiceDescription;
            serviceInstaller.ServicesDependedOn = new string[] { TestService.TestServiceDependsOn };
            Installers.Add(serviceInstaller);
            Installers.Add(processInstaller);
        }
    }

    public class TestService : ServiceBase
    {
        public const string TestServiceName = "TestServiceReplacementName";
        public const string TestServiceDisplayName = "TestServiceReplacementDisplayName";
        public const string TestServiceDescription = "TestServiceReplacementDescription";
        public const string TestServiceDependsOn = "TestServiceReplacementDependsOn";

        private string fileName = null;
        private System.ComponentModel.IContainer components = null;
        private bool failToStop = false;

        public TestService()
        {
            this.CanStop=true;
            this.CanPauseAndContinue=true;
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            if(args.Length == 1 && args[0] == "FailToStop")
            {
                this.failToStop=true;
                return;
            }

            if (args.Length == 0 || !Path.IsPathRooted(args[0]))
            {
                return;
            }

            this.fileName=args[0];
            using (StreamWriter writer = File.CreateText(fileName))
            {
                writer.WriteLine("Service started at {0}.", DateTime.Now);
                writer.WriteLine("Argument count: {0}.\r\nArguments:", args.Length);
                foreach (string arg in args)
                {
                    writer.WriteLine(" '{0}'", arg);
                }
            }
        }

        protected override void OnStop()
        {
            if(this.failToStop)
            {
                this.failToStop = false;
                throw new Exception("Will fail to stop");
            }

            if (this.fileName == null) { return; }
            using (StreamWriter writer = File.AppendText(this.fileName))
            {
                writer.WriteLine("Service stopped at {0}.", DateTime.Now);
            }
        }

        protected override void OnPause()
        {
            base.OnPause();
        }

        protected override void OnContinue()
        {
            base.OnContinue();
        }


        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            this.ServiceName = TestService.TestServiceName;
        }

        static void Main()
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new TestService()
            };
            ServiceBase.Run(ServicesToRun);
        }
    }
}