Composition handlers occasionally need to short-circuit a request and return an error response — for example, when an identifier fails validation or a resource is not found. Throwing an exception works, but it bypasses your API’s normal error-shape contract. ServiceComposer providesDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/ServiceComposer/ServiceComposer.AspNetCore/llms.txt
Use this file to discover all available pages before exploring further.
SetActionResult, an HttpRequest extension method that lets any handler set a first-class ASP.NET Core ActionResult instead, and ICompositionErrorsHandler for catching unhandled exceptions that escape the composition pipeline entirely.
When to use action results
Validation errors
Return
BadRequestObjectResult with ValidationProblemDetails when an incoming value does not meet the required format or business rules.Not found / forbidden
Return
NotFoundResult or ForbidResult without throwing — avoids polluting logs with expected error conditions.Deterministic behaviour
ServiceComposer guarantees only the first call to
SetActionResult takes effect. All subsequent calls from other handlers are silently ignored, making the outcome predictable even when handlers run in parallel.Structured problem details
Use
ProblemDetails or ValidationProblemDetails to emit RFC 7807-compliant responses that clients can reliably parse.Setting an action result
Callrequest.SetActionResult(result) inside any ICompositionRequestsHandler implementation. The method is an extension on HttpRequest, so no extra service injection is required.
ServiceComposer supports only one action result per request. If two or more composition handlers call
SetActionResult, only the first call succeeds. All subsequent calls are silently discarded.Required configuration
Action results depend on MVC output formatters. Enable them inAddViewModelComposition:
builder.Services.AddControllers()builder.Services.AddControllersAndViews()builder.Services.AddMvc()builder.Services.AddRazorPages()
Custom HTTP status codes
For cases where a fullActionResult is unnecessary — or when a handler is the sole authority on the response code for its route — you can set the status code directly on HttpContext.Response:
Prefer SetActionResult for error scenarios
The recommended pattern is to useSetActionResult rather than writing to Response.StatusCode directly:
Response.StatusCode directly is appropriate only when you are in a non-MVC endpoint context where action results are unavailable, or when only one handler will ever touch the status code for that route.
Handling composition exceptions with ICompositionErrorsHandler
ICompositionErrorsHandler is a composition hook that fires when an unhandled exception escapes from any handler during the composition pipeline. Implement it to log the error, set a fallback action result, or perform cleanup:
ICompositionErrorsHandler by inspecting the composition handlers already registered for the matched endpoint. A class participates in error handling only if it is also registered as a composition component — for example, by implementing ICompositionRequestsHandler alongside ICompositionErrorsHandler:
ICompositionRequestsHandler, the assembly scanner picks it up automatically — no additional DI registration is needed.