Getting Started with Developing Windows Services - Joseph Watson - E-Book

Getting Started with Developing Windows Services E-Book

Joseph Watson

0,0
2,99 €

oder
-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung


This title is one of the "Essentials" IT Books published by TechNet Publications Limited.
This Book is a very helpful practical guide for beginners in the topic , which can be used as a learning material for students pursuing their studies in undergraduate and graduate levels in universities and colleges and those who want to learn the topic via a short and complete resource.
We hope you find this book useful in shaping your future career.

This book will be available soon...

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Veröffentlichungsjahr: 2016

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Joseph Watson

Getting Started with Developing Windows Services

BookRix GmbH & Co. KG81371 Munich

Guide to Contents

Table of Contents

Who is this book for?

Introduction

Chapter 1   Windows Services development with .NET

Chapter 2   The Windows Event Log

Chapter 3   Service Installer

Chapter 4   Backup Files Service

Chapter 5   Deploying the service

Chapter 6   Creating a user interface to configure the service

General Summary

Conclusion

Detailed Table of Contents

Who is this book for?

Who is this book for?

This book is being written primarily for .NET developers who want to improve their applications by using Windows Services or who want to start in Windows Services development.

The book starts with a brief explanation about what Windows Services are and how they are managed in Windows. Then, it shows how to create a basic Windows Service project type using Visual Studio.

The rest of the book is intended to build a practical Windows Service project, step by step, and show the code needed for that purpose. Basic concepts about Windows Services and Windows log events are discussed too.

Finally, it explains how to deploy the service application created in the computer in which the service will be running, and gives suggestions about making the deployment process easier.

For the purposes of this book, all the sample code was written in C# and requires Visual Studio 2010 and .NET framework 3.5 minimum. The entire project discussed in this book can be downloaded here.

I hope that by the time they finish reading this book, developers can create Windows Services taking advantage of Visual Studio and C# capabilities and benefit from using them in their projects.

 

 

Introduction

Introduction

What is a Windows Service?

A Windows Service, formerly known as an NT Service, is an executable application that runs in its own Windows session and doesn’t show a user interface. It operates in the background and runs as long as Windows is running. It can also be configured to start when the operating system is started, and alternatively, can be started manually or by an event. Because a Windows Service operates in the context of its own dedicated user account, it can operate when a user is not logged on.

Windows Services administration

Windows administrators can manage services in several ways. These include the Sc.exe command line tool, Windows Power Shell, and the Services snap-in, which is found under Administrative Tools in the Windows Control Panel and shown in the following figure.

Figure 1: The Services Snap-in

The Services snap-in can connect to the local computer or a remote computer on the network, enabling users to do the following:

View a list of all installed services including service name, description, and configuration Start, stop, pause, or restart services Change the startup type. Acceptable types are: Automatic: The service starts at system startup.Automatic (delayed): The service starts a short while after the system has finished starting up.Manual: The service starts only when explicitly summoned.Disabled: The service is disabled and will not run. Change the user account context in which the service works. Configure actions that should be taken if a service fails. Inspect service dependencies. Export the list of services to a text file.

Note: The Automatic (delayed) option was introduced in Windows Vista in an attempt to reduce the boot-to-desktop time. However, not all services support delayed start.

Developing Windows Services

Windows Services can be developed using Visual Studio. In order to be a Windows Service, a program needs to be coded in a particular way using a supplied template for this purpose. That is, it must handle start, stop, and pause messages from the Service Control Manager, the component of Windows that is responsible for starting and stopping services.

Tip: Services must be created in a Windows Service application project under Visual Studio.

 

 

Chapter 1 Windows Services development with .NET

Chapter 1 Windows Services development with .NET

Getting started

How to create the project in Visual Studio

The first step in developing a Windows Service, after loading Microsoft Visual Studio, is to create a Windows Service type project. The following figure shows this kind of project dialog in Visual Studio.

Figure 2: The Visual Studio Windows Service project type

Windows Service project base line

The base line for a Windows Service project consists of two programs; one of them, called Program.cs, manages the application entry point. The other one, Service1.cs, encapsulates the service class definition. A developer can add as many programs as it needs in order to easily maintain code or to add special features to the project.

Customizing project base line

The project base line can be customized in order to fit a developer’s own needs. To do it in such way, you must rename the Solution, Project, Program.cs, and Service1.cs nodes. For the purposes of this book, the Windows Service project will be named monitorservice. The following figure shows the Visual Studio Solution Explorer before and after customization.

Figure 3: Solution Explorer before and after project customization

Tip: You can quickly rename elements in the Solution Explorer tree by right-clicking the desired node and applying the Rename command, from the Context pop-up menu. After you rename the action, the Visual Studio Refactoring Code Tool is fired.

Application entry point

Application entry point

Like many Visual Studio applications, a Windows Service needs an entry point in order to be executed. The following code sample shows the entry point for the project.

Code Sample 1

using System;

using System.Collections.Generic;

using System.Linq;

using System.ServiceProcess;

using System.Text;

using System.Threading.Tasks;

namespace monitorservice

{

    static class monitorservicemain

    {

        /// <summary>

        /// The main entry point for the application.

        /// </summary>

        static void Main()

        {

            ServiceBase[] ServicesToRun;

            ServicesToRun = new ServiceBase[] 

            { 

                new monitorservice() 

            };

            ServiceBase.Run(ServicesToRun);

        }

    }

}

 

This program creates an array named ServicesToRun. This array is based on the ServiceBase .NET class, and stores an instance of the monitorservice custom class. Monitorservice is also derived from the ServiceBase .NET class, and will manage the Windows Service that is being developed.

Once the array is created, the program calls the Run method of ServiceBase, passing the array as a parameter, and service execution starts.