what is the best way to develop a computer service

Encinas, Luis 20 Reputation points
2026-11-04T02:42:55.16+00:00

what is the best to start developing a computer program that will eventually become a windows server. I will like this program to have a windows form during the development.

Developer technologies | Windows Forms
0 comments No comments

Answer accepted by question author
Gatlin Le (WICLOUD CORPORATION) 1,385 Reputation points Microsoft External Staff Moderator
2026-11-04T04:07:10.5866667+00:00

Hi @Encinas, Luis ,

Thank you for reaching out. Using a form while you develop is a sensible plan, since it makes the logic much easier to test and watch before it runs in the background. I'm reading "windows server" in your question as a Windows service. The key is to keep your actual logic separate from both the form and the service, so the same code runs in either place and nothing needs to be rewritten when you switch. The form itself can't become part of the service, because since Windows Vista a service runs in an isolated session and cannot interact directly with the user.

The setup looks like this:

  1. Create a Class Library project and put all the real work there, for example in a class with one method that starts the work and another that stops it. When choosing templates, pick the ones without "(.NET Framework)" in the name and use the same .NET version for all three projects, so they can reference each other.
  2. Create a Windows Forms App project that references the library. During development, use it to start, stop, and watch your logic with buttons and labels.
  3. When the logic is ready, create a Worker Service project that references the same library and calls the same class. The form project is not part of what you deploy.

The tutorial Create Windows Service using BackgroundService walks through step 3 in full, including sample code, the Microsoft.Extensions.Hosting.WindowsServices package, and registering the service with sc.exe. In Visual Studio, the Worker Service template appears only after the ASP.NET and web development workload is installed, so if you don't see it, that is the likely reason.

If you later want a window to check on the service after it's deployed, the form can stay as a separate app that communicates with the service, as the Interactive Services page above describes.

I hope this gives you a clear starting point for the project. If any step doesn't go as expected, share which step it is and the exact error message you see, and I'm happy to help from there.

I hope this helps clarify the available documentation and the appropriate next step. If you found my response helpful or informative, I would greatly appreciate it if you could follow this guidance or provide your insights.

Thank you.

Was this answer helpful?

2 people found this answer helpful.

1 additional answer

Sort by: Oldest
  1. AI answer

    2026-11-04T09:59:21.1133333+00:00

    Use three projects—a reusable class library, a Windows Forms development application, and a .NET Worker Service—because a Windows Service cannot reliably display or host an interactive Windows Forms UI.

    Create a Visual Studio solution containing:

    1. MyApp.Core — Class Library
      • Contains the actual business logic.
      • Must not reference Windows Forms or service-specific APIs.
      • Exposes methods that both hosts can call.
    2. MyApp.DevUI — Windows Forms application
      • References MyApp.Core.
      • Provides buttons, status displays, and configuration controls during development.
      • Runs normally with F5.
    3. MyApp.Service — Worker Service
      • References MyApp.Core.
      • Executes the same logic in the background using BackgroundService.
      • Becomes the production Windows Service.

    This separation is important because Windows Services run in Session 0, outside the logged-on user’s interactive session. A form or dialog created by the service generally cannot be seen or used by the logged-on user.^1^

    1. Create the shared logic

    In Visual Studio, select File → New → Project → Class Library and create MyApp.Core.

    For example:

    namespace MyApp.Core;
    
    public sealed class JobProcessor
    {
        public async Task RunOnceAsync(CancellationToken cancellationToken)
        {
            // Put the real application work here:
            // read data, process files, call APIs, update a database, etc.
            await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
        }
    }
    

    Keep this library independent of both WinForms and BackgroundService. That makes it straightforward to test and reuse.

    2. Create the Windows Forms development host

    Add a Windows Forms App named MyApp.DevUI, then add a project reference to MyApp.Core:

    Right-click Dependencies → Add Project Reference → MyApp.Core

    A button can execute one cycle of the shared operation:

    using MyApp.Core;
    
    public partial class MainForm : Form
    {
        private readonly JobProcessor _processor = new();
    
        public MainForm() => InitializeComponent();
    
        private async void runButton_Click(object sender, EventArgs e)
        {
            statusLabel.Text = "Running...";
            await _processor.RunOnceAsync(CancellationToken.None);
            statusLabel.Text = "Completed";
        }
    }
    

    Use this application to develop and inspect behavior. Do not plan to move MainForm into the service project.

    3. Create the production Worker Service

    Add a Worker Service project named MyApp.Service and reference MyApp.Core. The Worker Service template is designed for long-running operations and provides logging, configuration, dependency injection, and cancellation support.^2^

    Implement the worker:

    using MyApp.Core;
    
    public sealed class Worker(JobProcessor processor) : BackgroundService
    {
        protected override async Task ExecuteAsync(
            CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                await processor.RunOnceAsync(stoppingToken);
                await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
            }
        }
    }
    

    Register the shared component and configure Windows Service hosting in Program.cs:

    using MyApp.Core;
    
    HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
    
    builder.Services.AddWindowsService(options =>
        options.ServiceName = "My Application Service");
    
    builder.Services.AddSingleton<JobProcessor>();
    builder.Services.AddHostedService<Worker>();
    
    builder.Build().Run();
    

    Install the Microsoft.Extensions.Hosting.WindowsServices NuGet package in the Worker project. Microsoft’s Windows Service tutorial documents the Worker template, package, publishing, installation, logging, and service lifecycle.^3^

    Development and deployment workflow

    1. Set MyApp.DevUI as the startup project while developing the processing logic.
    2. Test MyApp.Service as a normal Worker application with F5 or dotnet run.
    3. Publish and install it as a Windows Service only after the behavior is stable.
    4. Use structured logging or Windows Event Log rather than message boxes.
    5. If administrators need a UI after deployment, keep the WinForms program as a separate management application and have it communicate with the service through a defined IPC or API boundary.

    After installation, traditional service debugging requires starting the service through the Service Control Manager and attaching Visual Studio to its running process.^4^ Keeping the core logic runnable from WinForms or a normal Worker host avoids requiring that slower debugging cycle for most development.


    References

    1. Service Changes for Windows Vista
    2. Worker services in .NET
    3. Create Windows Service using BackgroundService
    4. How to: Debug Windows Service Applications
    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.