Skip to main content
All docs
V26.1
  • ExceptionService Class

    A service that allows you to customize how XAF-related exceptions are handled.

    Namespace: DevExpress.ExpressApp.Blazor.Services

    Assembly: DevExpress.ExpressApp.Blazor.v26.1.dll

    NuGet Package: DevExpress.ExpressApp.Blazor

    Declaration

    public class ExceptionService :
        IExceptionHandlerService,
        IExceptionProviderService

    Remarks

    The ExceptionService exposes two methods:

    ShouldHandleException(Exception)
    Override this method to specify whether the ExceptionService should process an exception.
    HandleException(Exception)
    Override this method to process an exception, for example, to wrap it in a user-friendly exception (IUserFriendlyException) or customize the user-friendly text.

    The following example displays a user-friendly exception message in your XAF Blazor UI application:

    1. Add a controller:

      using DevExpress.ExpressApp;
      using DevExpress.ExpressApp.Actions;
      using System;
      
      namespace MySolution.Module.Controllers {
          public class MyCustomException : Exception {
          }
      
          public class TestExceptionController : ViewController {
              public TestExceptionController() {
                  // When a user clicks this action, an exception is thrown.
                  SimpleAction simpleAction = new SimpleAction(this, "Test Action", DevExpress.Persistent.Base.PredefinedCategory.Edit);
                  simpleAction.Execute += SimpleAction_Execute;
              }
      
              private void SimpleAction_Execute(object sender, SimpleActionExecuteEventArgs e) {
                  throw new MyCustomException();
              }
          }
      }
      
    2. Add the MyCustomExceptionService service. Override the ShouldHandleException(Exception) method to specify that the service should process the MyCustomException exception, and override the HandleException(Exception) method to wrap this exception in a user-friendly exception:

      using DevExpress.Data.Exceptions;
      using DevExpress.ExpressApp.Blazor.Services;
      // ...
      public class MyCustomExceptionService : ExceptionService {
          public MyCustomExceptionService(ILogger<ExceptionService> logger) : base(logger) { }
          public override bool ShouldHandleException(Exception exception) {
              if(exception is MyCustomException) {
                  return true;
              }
              else {
                  return base.ShouldHandleException(exception);
              }
          }
          public override void HandleException(Exception exception) {
              Exception result = exception is MyCustomException ? new UserFriendlyException("My Test User Friendly Exception", exception) : exception;
              base.HandleException(result);
          }
      }
      
    3. Register the MyCustomExceptionService service in the MySolution.Blazor.Server\Startup.cs file:

      // ...
      services.AddXaf<MySolutionBlazorApplication>(Configuration);
      // Register the service.
      services.AddScoped<IExceptionHandlerService, MyCustomExceptionService>();
      // ...
      
    4. Run the application and click the Test Action button. The user-friendly exception is displayed in a toast notification.

      xaf blazor user friendly url

    For more information about the default error processing mechanism in XAF ASP.NET Core Blazor applications, refer to the following topic: Error Handling in XAF ASP.NET Core Blazor.

    Inheritance

    Object
    ExceptionService
    See Also