Service Application via Visual Studio ( Day 1 )

Background

Let us lay out a lithe Service Application using Visual Studio Community Edition.

Visual Studio

Overview

  1. Install Visual Studio Community Edition
  2. Create New Project ( Windows Service )
  3. Installer Module
    • Add Installer Module
    • Customize Installer Module
  4. App.Config
    • Add Application Configuration Items
  5. Application
    • Configuration Items
      • Read Configuration Items
      • Log Configuration Items
  6. Install Service
  7. Configure Service
  8. Start Service

Download Install

Download & Install Visual Studio Community Edition if you don’t have it yet.

Usage

Create New Project – Service Application

Outline

  1. Create New Project
    • Visual C#
      • Windows Desktop
        • Type :- Windows Service (.Net Framework )

Image

newProject.20181130.0836AM.PNG

Add Installer

Outline

Link

  1. In Solution Explorer, access Design view for the service for which you want to add an installation component.

  2. Click the background of the designer to select the service itself, rather than any of its contents.
  3. With the designer in focus, right-click, and then click Add Installer.
    • A new class, ProjectInstaller, and two installation components, ServiceProcessInstaller and ServiceInstaller, are added to your project, and property values for the service are copied to the components.
  4. Click the ServiceInstaller component and verify that the value of the ServiceName property is set to the same value as the ServiceName property on the service itself.
  5. To determine how your service will be started, click the ServiceInstaller component and set the StartType property to the appropriate value.

Customize Installer

Outline

  1. serviceInstaller
    • Solution Explorer
      • Customize Service Installer Property Sheet
    •  constructor
      • InitializeComponent()
        • Make sure that you preserve the call to InitializeComponent
      • Customize Service
        • serviceName
        • displayName
        • description

Solution Explorer

Service Installer Property Sheet

Outline
  1. Access Solution Explorer
    • Access Item :- serviceInstaller.cs
      • Access Properties page
        • Adjust Service Properties
Image

serviceInstaller.20181130.0634PM.PNG

 

Code


using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Threading.Tasks;

namespace journalBible
{
    [RunInstaller(true)]
    public partial class serviceJournalInstaller : System.Configuration.Install.Installer
    {

        static string appName = "journalBible";
        static string appDisplayName = "journal Bible";
        static string appDescription = "journal Bible Application ( Sample)";

        public serviceJournalInstaller()
        {

            InitializeComponent();

            this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;

            this.serviceInstaller1.ServiceName = appName;

            this.serviceInstaller1.DisplayName = appDisplayName;

            this.serviceInstaller1.Description = appDescription;

        }

        private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
        {

        }

        private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
        {

        }
    }
}

Configuration

App.Config

Outline

  1. Add appSettings section
    • add Keys
      • smtpServer
      • emailAddress

Code

App.Config.20181130.0235PM

Main Code

Outline

  1. serviceInstaller
    • Methods
      • OnStart
        • Initialize
          • configurationRead
            • read configuration data from app.config file
          • configurationLog
            • write configuration data into Application Event Log

Code


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

using System.Configuration;
using System.IO;
using System.Security;

namespace journalBible
{
    public partial class ServiceJournalBible : ServiceBase
    {

        static String strAppName;

        /*
         * Configuration App.Config - ID
        */
        static String ID_STMP_SERVER = "smtpServer";
        static String ID_EMAIL_ADDRESS = "emailAddress";

        /*
         * Configuration in App.Config - Variables
        */
        static String strSMTPServer;
        static String strEmailAddress;

        public ServiceJournalBible()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            initialize();
        }

        protected override void OnStop()
        {
        }

        protected void initialize()
        {

            try
            {

                configurationRead();

                configurationLog();

            }
            catch (Exception)
            {

            }

        }

        /*
         * Get Application Name
        */
        private String getAppName()
        {

            String strAppFriendlyName;
            String strAppName;

            strAppFriendlyName = AppDomain.CurrentDomain.FriendlyName;

            strAppName = Path.GetFileNameWithoutExtension(strAppFriendlyName);

            return (strAppName);

        }

        /*
         * https://docs.microsoft.com/en-us/dotnet/framework/windows-services/how-to-log-information-about-services
        */

        public System.Diagnostics.EventLog customLogging()
        {

            System.Diagnostics.EventLog objEventLog = new System.Diagnostics.EventLog();

            Boolean bEventLogSourceExist = false;

            // Turn off autologging
            this.AutoLog = false;

            strAppName = getAppName();

            // create an event source, specifying the name of a log that
            // does not currently exist to create a new, custom log
            bEventLogSourceExist = System.Diagnostics.EventLog.SourceExists(strAppName);

            if (bEventLogSourceExist == false)
            {
                System.Diagnostics.EventLog.CreateEventSource
                (
                      strAppName
                    , strAppName
                );

            }

            // configure the event log instance to use this source name
            objEventLog.Source = strAppName;

            //objEventLog.Log = strAppName;

            return (objEventLog);

        } //customLogging() 

        protected void configurationRead()
        {

            System.Collections.Specialized.NameValueCollection objAppSettings;

            objAppSettings = System.Configuration.ConfigurationManager.AppSettings;

            strSMTPServer = objAppSettings[ID_STMP_SERVER];

            strEmailAddress = objAppSettings[ID_EMAIL_ADDRESS];

        } //configurationRead()

        protected void configurationLog()
        {

            String strLog;

            EventLog objEventLog;

            objEventLog = customLogging();

            //Set Source Name
            //objEventLog.Source = strAppName;

            //Prepare Log Entry
            if (strSMTPServer != String.Empty)
            {
                strLog = ID_STMP_SERVER + " :- " + strSMTPServer;

                //Write Log Entry
                objEventLog.WriteEntry(strLog);
            }

            //Prepare Log Entry
            if (strEmailAddress != String.Empty)
            {

                strLog = ID_EMAIL_ADDRESS + " :- " + strEmailAddress;

                //Write Log Entry
                objEventLog.WriteEntry(strLog);

            }

        } //configurationLog()

    }
}

Install Service

Outline

  1. use InstallUtil.exe to install Service

Code

Syntax


C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe [exe-name]

Sample


setlocal

	set "_appInstallerFolder=C:\Windows\Microsoft.NET\Framework64\v4.0.30319"
	set "_appInstallerTargeted=InstallUtil.exe"

	set "_appInstaller=%_appInstallerFolder%\%_appInstallerTargeted%"

	set "_appTargetedFolder=bin\Release"
	set "_appTargetedModule=journalBible.exe"

	set "_appTargeted=%_appTargetedFolder%\%_appTargetedModule%"

	%_appInstaller% %_appTargeted%

endlocal

Source Control

GitHub

DanielAdeniji/svcjournalApp
Link

Referenced Work

  1. Microsoft
    1. Docs > .NET > .NET Framework > Developing Windows Service Applications
      • How to: Install and Uninstall Services
        Link
      • How to: Add Installers to Your Service Application
        Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s