ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • How to read appSettings JSON from Class Library in ASP.NET Core
  • Printing Support In Asp.Net Core
  • Images Upload REST API using ASP.NET Core
  • How to use IActionFilter, IAsyncActionFilter in ASP.NET Core MVC?
  • ASP.NET CORE - Blazor CRUD operation using ADO.NET
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

Develop and Install a Windows Service in .Net Core

.Net .Net Core about 8 years ago || 4/12/2018 || 6.9 K View

  • Hide

In this article we will see how to create a windows service and write certain data in a notepad with database connection. For this let know about windows ser first.

Windows Service

In Windows NT operating systems, windows service is a computer program that operates in the background. It is similar in a concept to a Unix daemon. A Windows service must confirm to the interface rules and protocols of the Service Control Manager, the component responsible for managing the Windows services.

Now let’s create a windows service.

  • Open Visual studio (In my case I’m using Visual Studio 2015 Update 3)>> File >> New Project
  • Navigate to Visual C# in the left side of Visual Studio Templates >> Windows >> Classic Desktop.
  • Choose windows service and provide a suitable name to the project.
  • Click on OK.

This will take a few second to create our project and the first screen appears like this:

Now let’s rename the service1 to FileCreationService.

Now click on”Click here to switch to view”in the FileCreationService and it will show us the code written in it.

Now let’s add some code here as shown in the code snippet below:

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.Threading;
using System.IO;
using System.Data.SqlClient;

namespace WindowsService4
{
    public partial class FileCreationService : ServiceBase
    {
        Thread th;
        bool IsRunning = false;
        public FileCreationService ()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
           

            //
            th = new Thread(DoThis);
            th.Start();
            IsRunning = true;
        }
        private void DoThis()
        {
            while (IsRunning)
            {
                 string connstr = "Data Source=ALOK-PC;Initial Catalog=GYB_DB;User Id=sa;Password=sa";
                SqlConnection conn = new SqlConnection(connstr);
                string sql = "SELECT RoleName From SDBRole";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "SDBRole");
                File.AppendAllText(@"E:\Sundar.txt", Environment.NewLine + "" +sql );

            }
        }

        protected override void OnStop()
        {
            IsRunning = false;
            th = null;
        }
    }
}

Here we have inherited our class from ServiceBase. Now lets call FileCreationService.cs in our program class as shown in the code snippet below:

Now let’s install the installer. To do so:

  • Go to FileCreationService in Solution Explorer.
  • Right Click >> Add Installer

This will add the ProjectInstaller.cs in our project and appears like this:

  • Now Right click on serviceInstaller1.
  • Properties >> We can change our ServiceName, Start Type

  • Now go to properties of serviceProcessInstaller1.
  • Change the Account to Local System.

We haven’t install our service yet. To install as a windows service we will install Visual Studio Installer. There is another way too but its little bit complicated. I find this way simpler to use. Now go to https://marketplace.visualstudio.com/items?itemName=VisualStudioClient.MicrosoftVisualStudio2017InstallerProjects and download the visual studio installer and install it closing the Visual Studio.

After installing visual studio installer, again open our project.

  • Right click on our project >> New Project

  • Other Project Types >> Visual Studio Installer >> Setup Project.
  • Provide a suitable name as shown in the figure below:

After adding the Setup Project out Solution Explorer looks like this:

Right click on FileCreationServiceSetup and Add >> Project Output >> Primary Output >> Click on OK.

Again right click on FileCreationServiceSetup >>View >> Custom Actions.

Now Right click on Install folder >> Add Custom Actions >> Double click on OK.

Now build the application . To build the solution we can right click in the solution and build the solution or click on build on the top of the Visual Studio and click on Build Solution or just press Ctrl +shift + B.

After building the solution now we need to build our Setup Project.

  • Right click on PostServiceSetup >> Build

This will take a few seconds. After completion of build ,

  • Right click on the PostServiceSetup..
  • Open Folder in File Explorer
  • Open folder Debug>> Double Click on setup.exe

  • Click Next >> Choose Everyone >> Next >> Next >> Close.
  • Now go to Run >> services.msc

And see if our service exists there or not.

We had provided FamousService as a name of our service. And as you can see there exists our service. Its on Automatic in Startup Type as we had defined.

Right click on the service and start the service to check whether there is problem or not. Service should start successfully. If not then you’ve probably missed some above steps.

Output:

 

  • 4
  • 0
  • 2
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

Alok Pandey

Alok Pandey

Love to share what I know.

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey