Overview
The WorkerService is a Windows Service that runs continuously in the background on Windows machines. It acts as the print server, hosting a WebSocket endpoint that accepts connections from web browsers and processes print jobs. Location:source/WorkerService/
Platform: Windows only (.NET 10.0)
Entry Point: source/WorkerService/Program.cs
Purpose
The WorkerService provides:- Always-On WebSocket Server - Listens on port 7000 for web client connections
- Print Job Processing - Handles incoming print requests from Appsiel web application
- Template Management - Accepts and stores template updates from remote clients
- Scale Data Broadcasting - Streams serial scale readings to subscribed clients
- Background Execution - Runs without user interaction, starts on system boot
Project Configuration
WorkerService.csproj:Microsoft.Extensions.Hosting- Generic host infrastructureMicrosoft.Extensions.Hosting.WindowsServices- Windows Service integration- Core and Infrastructure layers
Program.cs - Service Configuration
Location:source/WorkerService/Program.cs
Host Builder Setup
.UseWindowsService()- Enables Windows Service hosting- Creates default builder with logging, configuration, and DI
Dependency Injection Registration
The Program.cs registers all required services (source/WorkerService/Program.cs:10):- All services use Singleton lifetime for shared state and performance
Workeris registered as a HostedService to run in background
Service Startup
Worker.cs - Background Service
Location:source/WorkerService/Worker.cs
Class Definition
BackgroundService (from Microsoft.Extensions.Hosting)
- Provides lifecycle hooks for long-running operations
ExecuteAsyncmethod runs in background
ILogger<Worker>- Logging frameworkIWebSocketService- WebSocket server implementationITemplateRepository- Template storage
Constructor and Event Subscription
- OnClientConnected - Logs new WebSocket connections
- OnClientDisconnected - Logs client disconnections
- OnPrintJobReceived - Logs print job requests (actual processing happens in WebSocketService → PrintService)
- OnTemplateUpdateReceived - Saves template updates and sends results back
Template Update Handler
Location: source/WorkerService/Worker.cs:37- Receive template from web client
- Save to local storage via
ITemplateRepository - Send success/failure result back to client
- No user confirmation required (auto-save)
ExecuteAsync - Main Service Loop
Location: source/WorkerService/Worker.cs:74- Ensure Default Templates - Creates missing templates from defaults
- Start WebSocket Server - Binds to port 7000 and begins accepting connections
- Enter Main Loop - Logs heartbeat every minute
- Wait for Cancellation - Runs until service stop is requested
- Cancellation token is triggered
- Exit main loop
- Stop WebSocket server gracefully
- Service terminates
WebSocket Server Flow
Service Installation
The WorkerService is installed as a Windows Service using the Windows Service Manager (sc.exe) or installers.Installation Commands
Service Properties
- Service Name:
WorkerService(or custom name) - Display Name: Appsiel Print Manager Worker
- Startup Type: Automatic
- Run As: Local System or specific user account
- Recovery: Restart on failure
Communication Protocols
WebSocket Message Format
All messages are JSON objects with atype field:
Print Job Request:
Response Format
Print Job Result:Logging
The Worker uses both:- Microsoft.Extensions.Logging - Framework logging (ILogger<Worker>)
- ILoggingService - Custom logging to files
- Information - Normal operations
- Warning - Recoverable issues
- Error - Failures and exceptions
- Windows Event Log (when running as service)
- Console output (when running as console app)
- File logs via ILoggingService
Error Handling
Startup Errors
- Template initialization failures are logged but don’t stop the service
- WebSocket server failures are logged and service continues (with degraded functionality)
Runtime Errors
- Print job errors are caught and returned to client in PrintJobResult
- Template update errors are caught and returned in TemplateUpdateResult
- Client disconnections are handled gracefully
Recovery
- Service can be restarted via Windows Service Manager
- WebSocket server can be restarted without full service restart
- Individual print job failures don’t affect other jobs
Performance Considerations
- Singleton Services - Shared state across all requests
- Async/Await - Non-blocking I/O operations
- Concurrent Clients - Multiple simultaneous WebSocket connections
- Message Queue - Queued message processing in WebSocketService
- Background Processing - Print jobs processed asynchronously
Security
- Local Network Only - WebSocket binds to local addresses
- No Authentication - Designed for trusted environments
- Service Permissions - Runs with appropriate Windows permissions
- File Access - Controlled access to printer and template files
Monitoring
Service Status:- Check via Windows Service Manager
- Query via TrayApp
- Monitor via UI application (Windows platform service manager)
- WebSocket server IsRunning property
- Client count monitoring
- Log file analysis
Troubleshooting
Service Won’t Start
- Check Windows Event Viewer for errors
- Verify port 7000 is not in use
- Ensure service has proper permissions
- Check dependencies are installed
WebSocket Connection Failures
- Verify service is running
- Check firewall rules for port 7000
- Test with telnet/curl to port 7000
- Review WebSocket server logs
Print Jobs Failing
- Check printer connectivity
- Verify printer settings in configuration
- Review print service logs
- Test with simple document
Next Steps
- MAUI Application - UI for managing the service
- Core Components - Understanding the services used
- System Overview - Overall system architecture