Files
DP44/enriched-qwen3-coder-next/Common/DTS.Common.DBSyncService.md

58 lines
4.8 KiB
Markdown
Raw Normal View History

2026-04-17 14:55:32 -04:00
---
source_files:
- Common/DTS.Common.DBSyncService/ProjectInstaller.cs
- Common/DTS.Common.DBSyncService/Program.cs
- Common/DTS.Common.DBSyncService/DBSyncService.Designer.cs
- Common/DTS.Common.DBSyncService/DBSyncService.cs
- Common/DTS.Common.DBSyncService/ProjectInstaller.Designer.cs
generated_at: "2026-04-16T14:10:55.814640+00:00"
model: "zai-org/GLM-5-FP8"
schema_version: 1
sha256: "5d52988c06d02137"
---
# Documentation: DTS.Common.DBSyncService
## 1. Purpose
This module implements a Windows Service application named `DBSyncService`. It provides a background process capable of executing database synchronization tasks and system monitoring via a configurable timer. The module includes the service logic, installation scaffolding for the Windows Service Control Manager, and a command-line entry point.
## 2. Public Interface
### `Program` (static class)
* **`void Main()`**: The entry point for the application. It instantiates `DBSyncService` and passes it to `System.ServiceProcess.ServiceBase.Run` to start the service lifecycle.
### `ProjectInstaller` (class)
* **`ProjectInstaller()`**: Constructor for the installer class. Calls `InitializeComponent` to configure the service installation metadata.
* **`DBSyncServiceProcessInstaller`**: Instance of `System.ServiceProcess.ServiceProcessInstaller`. Configured with `Password` and `Username` set to `null` (defaults to Local System account).
* **`DBSyncServiceInstaller`**: Instance of `System.ServiceProcess.ServiceInstaller`. Configured with `ServiceName` set to `"DBSyncService"`.
### `DBSyncService` (class : `ServiceBase`)
* **`DBSyncService()`**: Constructor. Initializes the component, configures the Windows Event Log source (creating it if it doesn't exist using settings `Settings.Default.ServiceName` and `Settings.Default.Service`), and initializes a monitoring timer if `Settings.Default.Monitoring` is true.
* **`void OnTimer(object sender, ElapsedEventArgs args)`**: Event handler for the monitoring timer. Writes an informational entry "Monitoring the System" to the Event Log.
* **`void OnStart(string[] args)`**: Protected override. Called when the service starts. Logs "{ServiceName} started" to the Event Log.
* **`void OnStop()`**: Protected override. Called when the service stops. Logs "{ServiceName} stopped" to the Event Log.
* **`void OnCustomCommand(int command)`**: Protected override. Handles custom commands sent to the service.
* **`CustomCommands` (enum)**: Defines custom command constants:
* `SyncDb = 128`
* `Command2 = 129`
* `Command3 = 130`
## 3. Invariants
* **Event Log Source**: The Event Log source must exist or the application must have permissions to create it. The constructor attempts to create it using `EventLog.CreateEventSource` if it does not exist.
* **Timer Execution**: The monitoring timer is only instantiated and started if `Settings.Default.Monitoring` is `true`.
* **Custom Commands**: Custom commands must have values of 128 or higher, as defined in the `CustomCommands` enum, which aligns with Windows Service custom command requirements.
## 4. Dependencies
* **Internal Dependencies**:
* `DTS.Common.DBSyncService.Properties.Settings`: Used to access configuration values (`ServiceName`, `Service`, `Monitoring`, `Interval`).
* **External Dependencies**:
* `System.ServiceProcess`: Used for `ServiceBase` and installer classes.
* `System.Configuration.Install`: Used for the `Installer` base class.
* `System.Diagnostics`: Used for `EventLog` and `EventLogEntryType`.
* `System.Timers`: Used for the monitoring timer.
## 5. Gotchas
* **Timer Garbage Collection**: In `DBSyncService()`, the `Timer` instance is created as a local variable (`var timer = new Timer...`). It is not assigned to a class-level field. Since `System.Timers.Timer` is not kept alive by the system, it is eligible for Garbage Collection once the constructor finishes. This will cause the timer to stop firing unexpectedly (or never fire) after the first GC cycle.
* **Service Name Mismatch**: The `ServiceName` property is set to `"Service1"` in `DBSyncService.Designer.cs` (inside `InitializeComponent`), but the installer in `ProjectInstaller.Designer.cs` sets the service name to `"DBSyncService"`. This inconsistency can cause the service to fail to start or be managed correctly by the Service Control Manager, as the service name registered during installation will not match the name the `ServiceBase` instance expects.
* **Empty Command Handlers**: The `OnCustomCommand` method contains a switch statement for `SyncDb`, `Command2`, and `Command3`, but all case blocks are empty. The service currently acknowledges these commands but performs no action.
* **TODO Implementation**: The `OnTimer` method contains a `TODO` comment, indicating that the actual monitoring logic is not yet implemented.