Skip to content

How does this Work?

Internally, Capsule implements a message queue and run-to-completion semantics. This means that invocations are not executed directly but enqueued in a thread-safe channel and then processed one at a time (also known as turn-based concurrency).

When a capsule is instantiated, the following infrastructure is created:

  • a dedicated, thread-safe channel
  • a hull that implements the capsule interface and passes invocations to its synchronizer, which in turn passes them to the channel
  • an invocation loop that executes invocations read from the channel

To ensure the invocation loops remain active, they are registered with a capsule host. This is a service that runs and monitors the invocation loops. When using AddCapsuleHost() to register dependencies, the capsule host is started and stopped from CapsuleBackgroundService, an IHostedService implementation.