--- name: crestapps-core-foundations description: Skill for the CrestApps.Core foundation packages (CrestApps.Core.Abstractions, CrestApps.Core, CrestApps.Core.Infrastructure, CrestApps.Core.Support). Covers the CrestAppsCoreBuilder and feature builder layering, AddCoreServices, catalogs (IReadCatalog, ICatalog, INamedCatalog, ISourceCatalog, INamedSourceCatalog), catalog managers and the AddCatalogManagers pipeline, ICatalogEntryHandler and CatalogEntryHandlerBase lifecycle handlers with validation through ValidatingContext, QueryContext paging, multi-source binding sources (INamedCatalogSource, MultiSourceNamedCatalog), IStoreCommitter commit boundaries and filters, CookieAuthenticationAntiforgeryEndpointFilter with AntiforgeryEndpointFilterOptions, IUserAccessor for hub-safe caller resolution, IODataValidator, and shared utilities such as UniqueId, JSOptions, RedactedSecret and DataProtectionHelper. Use it when building custom catalogs, stores, handlers, or infrastructure on CrestApps.Core. license: Apache-2.0 metadata: author: CrestApps Team version: "1.0" --- # CrestApps.Core Foundations You are a CrestApps.Core expert. Build on the framework contracts rather than on a specific store or provider. ## Package Layering | Layer | Package | Contents | |---|---|---| | Abstractions | `CrestApps.Core.Abstractions` | `ExtensibleEntity`, `CatalogItem`, `SourceCatalogEntry`, `INameAwareModel`, `ISourceAwareModel`, `IDisplayTextAwareModel`, catalog and manager interfaces, handler contexts, `QueryContext`, `PageResult`, `IStoreCommitter`, `IUserAccessor`, `UniqueId`, the feature builder classes | | Core | `CrestApps.Core` | `AddCrestAppsCore(...)`, `AddCoreServices()`, `AddCatalogManagers()`, catalog managers, `CatalogEntryHandlerBase`, multi-source catalogs, store-committer filters, `ODataFilterValidator`, `UserAccessor`, `JSOptions`, `DataProtectionHelper` | | Infrastructure | `CrestApps.Core.Infrastructure.Abstractions`, `CrestApps.Core.Infrastructure` | indexing contracts (`ISearchIndexManager`, `IIndexProfileHandler`), `DataSourceConstants`, `DocumentIndexConstants`, `RedactedSecret`, `DictionaryExtensions` | | Utilities | `CrestApps.Core.Support` | string, enum, date and JSON helpers | `AddCrestAppsCore(...)` creates the root `CrestAppsCoreBuilder` and registers `ExtensibleEntityJsonOptions` plus the hosted service that applies them. `AddIndexingServices(...)` on the root creates `CrestAppsIndexingBuilder`. `AddAISuite(...)` creates `CrestAppsAISuiteBuilder`, which hosts the feature builders (`CrestAppsChatInteractionsBuilder`, `CrestAppsDocumentProcessingBuilder`, `CrestAppsDocumentIngestionBuilder`, `CrestAppsAIMemoryBuilder`, `CrestAppsAIPersonasBuilder`, `CrestAppsAIToolInstancesBuilder`, `CrestAppsA2AClientBuilder`, `CrestAppsMcpClientBuilder`, `CrestAppsMcpServerBuilder`). Every builder method maps to an `IServiceCollection` extension such as `AddCoreAIServices()`, so a host can still call the lower-level methods directly. `AddCoreServices()` registers scoped `IODataValidator` (`ODataFilterValidator`), singleton `IHttpContextAccessor` and singleton `IUserAccessor` (`UserAccessor`). `AddAISuite(...)` calls it for you. ## Catalogs and Managers - `IReadCatalog`: `FindByIdAsync`, `GetAllAsync`, `GetAsync(ids)`, `PageAsync(page, pageSize, queryContext)`. - `ICatalog`: adds `CreateAsync`, `UpdateAsync`, `DeleteAsync`. - `INamedCatalog`: adds `FindByNameAsync`. `ISourceCatalog`: adds `GetAsync(source)`. `INamedSourceCatalog`: adds `GetAsync(name, source)`. - Every member takes an optional `CancellationToken`. - Resolve a catalog for raw persistence. Resolve a manager (`ICatalogManager`, `INamedCatalogManager`, `ISourceCatalogManager`, `INamedSourceCatalogManager`) when handlers must run. `AddCatalogManagers()` registers the open-generic managers; both store packages call it. - Managers run handlers in this order: `NewAsync` runs `Initializing` then `Initialized` and assigns `ItemId` with `UniqueId.GenerateId()`; `ValidateAsync` runs `Validating` then `Validated`; `CreateAsync`, `UpdateAsync` and `DeleteAsync` run their before and after events around the catalog call; reads run `Loaded`. - `CreateAsync` and `UpdateAsync` do **not** validate. Call `ValidateAsync` first and stop when `Succeeded` is false. - Handler exceptions are caught and logged by `HandlerExtensions.InvokeAsync`, so throwing in a handler never blocks a save. Report problems through `ValidatingContext.Result.Fail(...)`. - `QueryContext` has `Name`, `Source` and `Sorted`. `PageResult` has `Count` and `Entries`. ### Custom Model, Handler, and Manager ```csharp using System.ComponentModel.DataAnnotations; using CrestApps.Core; using CrestApps.Core.Handlers; using CrestApps.Core.Models; using CrestApps.Core.Services; namespace MyApp.Catalogs; public sealed class TicketCategory : CatalogItem, INameAwareModel { public string Name { get; set; } public string Description { get; set; } } public sealed class TicketCategoryHandler : CatalogEntryHandlerBase { private readonly INamedCatalog _catalog; public TicketCategoryHandler(INamedCatalog catalog) { _catalog = catalog; } public override async Task ValidatingAsync( ValidatingContext context, CancellationToken cancellationToken = default) { if (string.IsNullOrWhiteSpace(context.Model.Name)) { context.Result.Fail(new ValidationResult("A name is required.", [nameof(TicketCategory.Name)])); return; } var existing = await _catalog.FindByNameAsync(context.Model.Name, cancellationToken); if (existing is not null && existing.ItemId != context.Model.ItemId) { context.Result.Fail(new ValidationResult("The name is already used.", [nameof(TicketCategory.Name)])); } } } public sealed class TicketCategoryService { private readonly INamedCatalogManager _manager; public TicketCategoryService(INamedCatalogManager manager) { _manager = manager; } public async Task CreateAsync( string name, string description, CancellationToken cancellationToken = default) { var category = await _manager.NewAsync(name, cancellationToken: cancellationToken); category.Description = description; var result = await _manager.ValidateAsync(category, cancellationToken); if (result.Succeeded) { await _manager.CreateAsync(category, cancellationToken); } return result; } } ``` Register the catalog with the store package the host uses, then the handler and service: ```csharp using CrestApps.Core; using CrestApps.Core.Data.EntityCore.Services; // Entity Framework Core (CrestApps.Core.Data.EntityCore) builder.Services.AddNamedDocumentCatalog>(); // or YesSql (CrestApps.Core.Data.YesSql); also register the index provider and create its table // builder.Services.AddYesSqlNamedDocumentCatalog("App"); builder.Services.AddScoped, TicketCategoryHandler>(); builder.Services.AddScoped(); ``` `AddDocumentCatalog`, `AddNamedDocumentCatalog`, `AddSourceDocumentCatalog` and `AddNamedSourceDocumentCatalog` are defined in `CrestApps.Core.Abstractions` and take the model and the concrete catalog type. See `crestapps-core-entitycore-stores` and `crestapps-core-yessql-stores` for the backend-specific catalog types and indexes. ## Multi-Source Binding Sources Connections and deployments merge configuration, database, and custom sources. `MultiSourceNamedCatalog` and `MultiSourceNamedSourceCatalog` order the registered `INamedCatalogSource` or `INamedSourceCatalogSource` instances by `Order`, keep the first entry for each name, and send writes to the first writable source (`IWritableNamedCatalogSource`). Database sources use `Order = 0`, configuration sources `Order = 100`. ```csharp using CrestApps.Core.AI.Models; using CrestApps.Core.Services; namespace MyApp.Deployments; public sealed class PlatformDeploymentSource : INamedSourceCatalogSource { private readonly IPlatformDeploymentClient _client; public PlatformDeploymentSource(IPlatformDeploymentClient client) { _client = client; } public int Order => 50; public async ValueTask> GetEntriesAsync( IReadOnlyCollection knownEntries, CancellationToken cancellationToken = default) { var knownNames = knownEntries .Select(entry => entry.Name) .ToHashSet(StringComparer.OrdinalIgnoreCase); var deployments = await _client.GetDeploymentsAsync(cancellationToken); return deployments .Where(deployment => !knownNames.Contains(deployment.Name)) .ToArray(); } } ``` ```csharp builder.Services.TryAddEnumerable( ServiceDescriptor.Scoped, PlatformDeploymentSource>()); ``` `WritableCatalogBindingSource` and `WritableNamedCatalogBindingSource` wrap an existing catalog as an `Order = 0` writable source; the store packages use them in `AddEntityCoreNamedSourceBindingSource()` and `AddYesSqlNamedSourceBindingSource()`. ## Commit Boundaries The first-party stores stage writes and flush them through scoped `IStoreCommitter.CommitAsync()`. - MVC: `AddControllersWithViews().AddCrestAppsStoreCommitterFilter()` commits after each action that did not throw. - Minimal APIs: `.AddEndpointFilter()` (namespace `CrestApps.Core.Filters`) commits after the handler. - SignalR: `AddSignalR().AddCrestAppsStoreCommitterFilter()`, or `.AddSignalR(addStoreCommitterFilter: true)` on the AI suite builder. - Background work: create a scope, write, then call `CommitAsync()` on that scope's `IStoreCommitter`. ### Antiforgery for Minimal APIs `CookieAuthenticationAntiforgeryEndpointFilter` (namespace `CrestApps.Core.Filters`) validates the antiforgery token (the `RequestVerificationToken` header or the form field) of a request whose credential the browser attaches by itself: a cookie, or the `Authorization` header of Negotiate, NTLM and Basic. A request needs no token only when every authenticated identity has an authentication type in `AntiforgeryEndpointFilterOptions.NonCookieAuthenticationTypes` (`Bearer`, `Api`, `ApiKey`, `OpenIddict.Validation.AspNetCore` by default; it is the identity's authentication type, not the scheme name), or when it is anonymous and carries none of the `AmbientCookieNames`. Keep `DisableAntiforgery()` on the endpoint so the middleware does not validate twice, and register `AddAntiforgery()`. ```csharp using CrestApps.Core.Filters; builder.Services.Configure(options => { options.NonCookieAuthenticationTypes.Add("MyApiKey"); // ClaimsIdentity.AuthenticationType your API handler sets }); app.MapPost("/api/my-upload", HandleUploadAsync) .DisableAntiforgery() .AddEndpointFilter(); ``` The chat document upload and remove endpoints use it (see `crestapps-core-chat-interactions`). - The filters resolve `IStoreCommitter` from DI. `NoOpStoreCommitter` is internal and is never registered, so register a store package (or your own scoped committer) before adding a filter. ## Caller Identity Resolve the caller through `IUserAccessor.User`, not `IHttpContextAccessor`. SignalR hub methods run outside the request pipeline, so `HttpContext` is often null there. The default `UserAccessor` returns the principal a hub assigned for the current invocation and falls back to the HTTP request principal. A null `User` means a trusted server-side invocation (for example a background task), not an anonymous caller. ## OData Filters Validate user-supplied filters with `IODataValidator.IsValidFilter(filter)` before handing them to an `IODataFilterTranslator`. The validator checks syntax only; the search backend performs full validation. Replace it with `services.AddScoped()` for stricter rules such as a field allow-list. ## Utilities - `UniqueId.GenerateId()` and `UniqueId.IsValid(id)` create and check the 26-character item identifiers. - `JSOptions.Default`, `JSOptions.CaseInsensitive` and `JSOptions.Indented` are shared read-only `JsonSerializerOptions`. - `JsonExtensions.FromObject(...)` and the `Clone()` helpers copy property bags and `JsonObject` values. - `DataProtectionHelper.Unprotect(protector, value, logger, message)` unprotects a secret and logs instead of throwing. - `RedactedSecret` keeps secrets out of logs and UI output. - `DataSourceConstants.ColumnNames` names the data-source knowledge index fields and `DocumentIndexConstants.ColumnNames` the uploaded-document chunk fields; they are different index schemas. ## Upgrading Within 2.0 Previews - The packages target `net10.0` and `net11.0`. On `net11.0` the `Microsoft.Extensions.*` abstractions they reference resolve to 11.0.0; on `net10.0` they stay on 10.0.x. ## Upgrading from 1.x - Every type now lives in a file named after it (226 types moved, for example each YesSql `*Index` and `*IndexProvider`). Namespaces and members did not change, so code that compiles against 1.x still compiles. - Feature builders added in 2.0 include `CrestAppsDocumentIngestionBuilder` and `CrestAppsAIPersonasBuilder`.