Implement windows service in .NET 6.0

Sachi 221 Reputation points
2021-12-16T07:49:21.173+00:00

Hi Team
I want to create

  1. Sample window service in .Net framework and install it.
  2. And Need to port this window service to .net 6.0
    Help me to solve this.
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,375 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,131 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jaliya Udagedara 2,736 Reputation points MVP
    2021-12-16T07:58:16.16+00:00

    For Windows Services on .NET Framework,

    For Windows Services on .NET 6 ,

    2 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Sachi 221 Reputation points
    2021-12-16T15:25:26.877+00:00

    While installing service I'm getting some exceptions saying "assembly not found from the path or incorrect format "

    0 comments No comments

  2. Sachi 221 Reputation points
    2021-12-18T05:04:06.81+00:00

    I have created a window service by using
    https://learn.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer

    Here they have used some win32 calls I do not understand that please can u explain why they used and what the functionality of those win32 calls for service

    using System.Runtime.InteropServices;

    public enum ServiceState
    {
    SERVICE_STOPPED = 0x00000001,
    SERVICE_START_PENDING = 0x00000002,
    SERVICE_STOP_PENDING = 0x00000003,
    SERVICE_RUNNING = 0x00000004,
    SERVICE_CONTINUE_PENDING = 0x00000005,
    SERVICE_PAUSE_PENDING = 0x00000006,
    SERVICE_PAUSED = 0x00000007,
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct ServiceStatus
    {
    public int dwServiceType;
    public ServiceState dwCurrentState;
    public int dwControlsAccepted;
    public int dwWin32ExitCode;
    public int dwServiceSpecificExitCode;
    public int dwCheckPoint;
    public int dwWaitHint;
    };
    [DllImport("advapi32.dll", SetLastError = true)]
    private static extern bool SetServiceStatus(System.IntPtr handle, ref ServiceStatus serviceStatus);

    // Update the service state to Start Pending.
    ServiceStatus serviceStatus = new ServiceStatus();
    serviceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING;
    serviceStatus.dwWaitHint = 100000;
    SetServiceStatus(this.ServiceHandle, ref serviceStatus);

    // Update the service state to Running.
    serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING;
    SetServiceStatus(this.ServiceHandle, ref serviceStatus);

    Pls explain what this will do ?