# Daemon Services

In SanteDB, a daemon service represents a service which is activated on startup and is shut down on service stop. Daemon services can exist on the iCDR and dCDR interfaces. Daemon services are implemented using the [`IDaemonService`](broken://pages/-MDKigCWfp3J-U1IFw1q)

Daemons are particularly useful when:

* The plugin needs to subscribe to events on other services
* The plugin needs to register / configure other services on application start
* The plugin wants a long-running background task (like a messaging interface, listener, etc.)

### Lifecycle

Daemons go through the same lifecycle as the primary application host. Daemons have their `.Start()` method called at Stage 2 of the application startup and their `.Stop()` method called when the application host is shutting down.

## Implementation

### Daemon Scaffolding

Implementing the IDaemonService interface is all that is required to indicate a particular implementation is a daemon.

```csharp
public class HelloDaemon : IDaemonService
{

    public bool IsRunning => false;

    public string ServiceName => "HelloWorld Daemon";

    public event EventHandler Starting;
    public event EventHandler Started;
    public event EventHandler Stopping;
    public event EventHandler Stopped;

    public bool Start()
    {
        // Start the daemon
    }

    public bool Stop()
    {
        // Stop the daemon
    }
}
```

### Events

The IDaemonService implementation has four events which daemons are expected to raise. These events allow other modules/services to detect when your daemon has completed the necessary steps to start its services. For example, you may wish to have your daemon only start after a repository service is finished starting up, etc. The events are:

| Event    | Description                                                                                                                                         |
| -------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| Starting | This event should be fired when your daemon has initiated its startup process.                                                                      |
| Started  | This event should be fired after your daemon service has completed all necessary startup processes.                                                 |
| Stopping | This event should be fired when your daemon has initiated shutdown. This allows other services to handle that your service(s) may not be available. |
| Stopped  | This event should be fired after your daemon has finished its shutdown process.                                                                     |

### Current Status

You should allow other services to detect whether your daemon has completed startup and is currently active. This state is indicated by the `IsRunning` property.

### Start/Stop Methods

The `Start()` method should, at minimum be implemented as:

```csharp
public bool Start() {
    // Notify of startup
    this.Starting?.Invoke(this, EventArgs.Empty);
    
    // Do your startup stuff here
    
    // Notify that startup has completed
    this.Started?.Invoke(this, EventArgs.Empty);
    
    // Finally, let the application host know you started up 
    return true;
}
```

The Stop() method is implemented in a similar manner:

```csharp
public bool Stop() {
    // Notify of stop
    this.Stopping?.Invoke(this, EventArgs.Empty);
    
    // Do your shutdown stuff
    
    // Notify that shutdown hsa stopped
    this.Stopped?.Invoke(this, EventArgs.Empty);
    
    // Finally let the application host know you shutdown properly
    return true;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.santesuite.org/developers/server-plugins/implementing-.net-features/daemon-services.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
