> For the complete documentation index, see [llms.txt](https://brightercommand.gitbook.io/paramore-brighter-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://brightercommand.gitbook.io/paramore-brighter-documentation/darker/darkerbasicconfiguration/darkerconfigurationreference.md).

# Darker Configuration Reference

The options AddDarker and AddHandlersFromAssemblies take: the query processor's service lifetime, and the strategies available for registering handlers.

> **Reference** · Applies to **Darker V4** · Prerequisites: [Basic Configuration](/paramore-brighter-documentation/darker/darkerbasicconfiguration.md)

The options `AddDarker` and `AddHandlersFromAssemblies` take: the query processor's service lifetime, and the strategies available for registering handlers. For the configuration these options sit inside, see [Basic Configuration](/paramore-brighter-documentation/darker/darkerbasicconfiguration.md).

## Darker Query Processor Lifetime

By default, the `IQueryProcessor` is registered with a **Transient** lifetime, meaning a new instance is created each time it's requested. However, if you're using Entity Framework Core, you need to register the Query Processor with a **Scoped** lifetime to match the EF Core DbContext lifetime.

**Default Configuration (Transient):**

```csharp
// ...
builder.Services.AddDarker()
    .AddHandlersFromAssemblies(typeof(Program).Assembly);
```

**Scoped Configuration (Required for EF Core):**

When using Entity Framework Core, the DbContext is registered as scoped by default. To ensure Darker works correctly with EF Core, you must configure the Query Processor to use the same scoped lifetime:

```csharp
using Microsoft.Extensions.DependencyInjection;
using Paramore.Darker;
using Paramore.Darker.AspNetCore;

builder.Services.AddDarker(options =>
{
    // EFCore registers DbContext as scoped
    // Query Processor must also be scoped to work with EF Core
    options.QueryProcessorLifetime = ServiceLifetime.Scoped;
})
.AddHandlersFromAssemblies(typeof(Program).Assembly);
```

If you don't configure the scoped lifetime when using EF Core, you may encounter exceptions related to accessing a disposed DbContext.

## Darker Handler Registration Strategies

Darker provides two ways to register query handlers: automatic assembly scanning (recommended) and manual registration.

**Assembly Scanning (Recommended):**

The `AddHandlersFromAssemblies` method scans one or more assemblies and automatically registers all query handlers it finds:

```csharp
// ...
// Scan a single assembly
builder.Services.AddDarker()
    .AddHandlersFromAssemblies(typeof(GetPeopleQuery).Assembly);

// Scan multiple assemblies
builder.Services.AddDarker()
    .AddHandlersFromAssemblies(
        typeof(GetPeopleQuery).Assembly,
        typeof(GetOrdersQuery).Assembly);
```

This approach follows convention over configuration and is the easiest way to register handlers.

**Manual Registration:**

For more control over handler registration, you can use `QueryHandlerRegistry` to register handlers explicitly:

```csharp
using Paramore.Darker;
using Paramore.Darker.Builder;

var registry = new QueryHandlerRegistry();
registry.Register<GetPeopleQuery, IReadOnlyDictionary<int, string>, GetPeopleQueryHandler>();
registry.Register<GetPersonNameQuery, string, GetPersonQueryHandler>();

IQueryProcessor queryProcessor = QueryProcessorBuilder.With()
    .Handlers(registry, Activator.CreateInstance, t => {}, Activator.CreateInstance)
    .InMemoryQueryContextFactory()
    .Build();
```

Manual registration is useful when you need fine-grained control over which handlers are registered or when you're not using ASP.NET Core's dependency injection.

## Further Reading

* [Basic Configuration](/paramore-brighter-documentation/darker/darkerbasicconfiguration.md) - The configuration these options sit inside
* [Query Pipeline and Decorators](/paramore-brighter-documentation/darker/querypipeline.md) - Configuring decorators and policies
* [Implementing a Query Handler](/paramore-brighter-documentation/darker/implementaqueryhandler.md) - The handlers being registered


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://brightercommand.gitbook.io/paramore-brighter-documentation/darker/darkerbasicconfiguration/darkerconfigurationreference.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
