GithubHelp home page GithubHelp logo

kbmanikanta / backgroundservicedemo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from luisalvar/backgroundservicedemo

0.0 0.0 0.0 15 KB

.NET 6 deploy a Windows Service

License: MIT License

C# 90.22% PowerShell 9.78%

backgroundservicedemo's Introduction

.NET 6.0 Worker Serivce Publish as A Windows Service

Main Source: Create a Windows Service Using Background Service

.NET Framework developers are probably familiar with Windows Service apps. Before .NET Core and .NET 5+, developers who relied on .NET Framework could create Windows Services to perform background tasks or execute long-running processes. This functionality is still available and you can create Worker Services that run as a Windows Service.

We start first by creating the solution file for the

dotnet new sln --name BackgroundServiceDemo

Next, we get the template for the Worker Service

dotnet new worker --name BackgroundServiceDemo --framework "net6.0"

Next, add the woker service to the solution file

dotnet sln add **/*.csproj BackgroundServiceDemo.sln

Add the following NuGet Packages:

  • Microsoft.Extensions.Hosting.WindowsServices
  • Microsoft.Extensions.Http
dotnet add package Microsoft.Extensions.Http --version 6.0.0

Publish the app

Important Information

To create the .NET Worker Servicen as a Windows Service, it's recommended that you publish the app as a single file executable. It's less error-prone to have a self-contained executable, as there aren't any dependent files lying around the file system.

In this case, I will publish as an .exe.

    <OutputType>exe</OutputType>
    <PublishSingleFile Condition="'$(Configuration)' == 'Release'">true</PublishSingleFile>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PlatformTarget>x64</PlatformTarget>

The project file now contains the following information

  • create a console application
  • Enable single-file publishing
  • Runtime Id as windows x64
  • target platform CPU of 64-bit
dotnet publish --output ./app/publish

Create the Windows Service

echo "Set-Location"
$RootFolderPath = Get-Location 
$PublishDLLPath = "app/publish/"
$TargetDLL = "BackgroundServiceDemo.exe"
$FullPath = join-path -path $RootFolderPath -childpath $PublishDLLPath
$FullPath = ($FullPath + $TargetDLL)
sc.exe create ".NET Joke Service" binpath=$FullPath

How to run the powershell script: powershell -executionpolicy bypass -File .\CreateWindowsService.ps1

Configure The Windows Service

Optional, if we are okay with the service defaults. Windows Services provide recovery configuraiton options.

  • Query the current configuraiton using the following; outputs the recovery configuration
      sc.exe qfailure ".NET Joke Service"
  • To configure recover user the failure option not the qfailure
      sc.exe failure ".NET Joke Service" reset=0 actions=restart/60000/restart/60000/run/1000

backgroundservicedemo's People

Contributors

luisalvar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.