Skip to content

Conversation

@StefanOssendorf
Copy link
Contributor

A new API is added to make it possible to handle exceptions thrown by asynchronous rules.
The new interface to implement is Csla.Rules.IUnhandledAsyncRuleExceptionHandler which has two methods

  • bool CanHandle(Exception, IBusinessRuleBase)

    • to decide whether this exception should be handled or not
  • ValueTask Handle(Exception, IBusinessRuleBase, IRuleContext)

    • to handle the exception when CanHandle(...) == true
      With these methods you can now decide whether to handle the exception and how or let the exception be unobserved bubble up and potentially cause a crash.

You can register your implementation in two ways

  • Just add the implementation to your service collection services.AddScoped<IUnhandledAsyncRuleExceptionHandler, YourImplementation>()
  • Use services.AddCsla(o => o.UseUnhandledAsyncRuleExceptionHandler<YourImplementation>());. The handler is registered as scoped.

The default is still no handling of any exception thrown in an asynchronous rule.

Fixes #4725

Add a new interface IUnhandledAsyncRuleExceptionHandler so a user can inspect and do somethign with exceptions raised by async rules.
@rockfordlhotka rockfordlhotka requested a review from Copilot October 3, 2025 22:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a new API to handle exceptions thrown by asynchronous business rules in CSLA. It introduces the IUnhandledAsyncRuleExceptionHandler interface that allows developers to decide whether to handle specific async rule exceptions and how to handle them.

Key changes:

  • New interface IUnhandledAsyncRuleExceptionHandler with CanHandle and Handle methods
  • Integration of exception handling into the async rule execution pipeline
  • Configuration options to register custom exception handlers via dependency injection

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
docs/Upgrading to CSLA 10.md Documents the new async rule exception handling API and breaking changes
Source/Csla/Rules/IUnhandledAsyncRuleExceptionHandler.cs Defines the new interface for handling async rule exceptions
Source/Csla/Rules/DontObserveUnhandledAsyncRuleExceptionHandler.cs Default implementation that doesn't handle any exceptions
Source/Csla/Rules/BusinessRules.cs Integrates exception handler into async rule execution
Source/Csla/Core/BusinessBase.cs Updates BusinessRules instantiation to include exception handler
Source/Csla/Configuration/Fluent/CslaOptions.cs Adds configuration option for custom exception handlers
Source/Csla/Configuration/ConfigurationExtensions.cs Registers exception handler service in DI container
Source/tests/Csla.test/ValidationRules/TestUnhandledAsyncRuleExceptionHandler.cs Test implementation of the exception handler interface
Source/tests/Csla.test/ValidationRules/DelayedAsynRuleExceptionRoot.cs Test class that creates async rules with exceptions
Source/tests/Csla.test/ValidationRules/AsyncRuleTests.cs Test cases for async rule exception handling
Source/tests/Csla.test/Csla.Tests.csproj Adds project references for code generators

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@rockfordlhotka rockfordlhotka merged commit 6d71ea6 into main Oct 4, 2025
3 checks passed
@rockfordlhotka rockfordlhotka deleted the fix/4725-handle-uncatched-async-rule-exceptions branch October 4, 2025 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Async rules can terminate the process when not being handled

3 participants