SnapExit 2.2.4

Additional Details

This package was discontinued due to limitations in Task programming causing it to produce many memory leaks.

There is a newer version of this package available.
See the version list below for details.
dotnet add package SnapExit --version 2.2.4
                    
NuGet\Install-Package SnapExit -Version 2.2.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SnapExit" Version="2.2.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SnapExit" Version="2.2.4" />
                    
Directory.Packages.props
<PackageReference Include="SnapExit" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SnapExit --version 2.2.4
                    
#r "nuget: SnapExit, 2.2.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package SnapExit@2.2.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SnapExit&version=2.2.4
                    
Install as a Cake Addin
#tool nuget:?package=SnapExit&version=2.2.4
                    
Install as a Cake Tool

Snap exit

nuget link: SnapExit

A nuget package that allows for exception-like behavior to validate state in any ASP.NET project, but with all the performance benefits of cancellation tokens.

Any feedback is helpful. Please leave it in the Issues section.

Performance

The package is meant to replace exceptions but still keeps (or improves) the performance of IActionResult

Exceptions IActionResult SnapExit
200ms 60ms 60ms

*Results may vary based on use case. but expect a minimum 2x improvement over exceptions

Usage

Register SnapExit in your Program.cs

    builder.Services.AddSnapExit(); // registers the services

Inject the ExecutionControlService into your flow and use it like you would an exception:

  public class YourService {
      private readonly IExecutionControlService _executionControlService;
      public YourService(IExecutionControlService executionControlService) {
          _executionControlService = executionControlService;
      }

      // New way
      public async Task ThisShouldThrow() {
          await _executionControlService.StopExecution();
      }

      // Old way
      public Task ThisShouldThrow() {
          throw new Exception();
      }
  }

This halts the flow of task immediatly, you can even return custom defined data to the return point:

  _executionControlService.StopExecution(new {
      // any response data can be passed here
  });

To make this work you will need to define a return point for SnapExit. This is made easy by using the SnapExitManager base class!

  public class SnapExitReturnPoint : SnapExitManager<ResponseData, EnviroumentData> // the generics are to be implemented by you
  {
      public void ThisCanBeAnyPointOfCode() {
          onSnapExit += OnSnapExit; // register the callback function or use the virtual protected callback
          Task task = SomeLongTask();
          RegisterSnapExit(task);
      }

      private Task OnSnapExit(ResponseData responseData, EnviroumentData enviroumentData) { // response and enviroument is data passed at error time and register time
          // Do some code here related to an exit
      }
  }

Also look at the example to see how you can turn it into a middleware for ASP.NET Core api!

Recommendation

I recommend creating your own service with specialized responses based on events. For example:

public class ErrorService {
    private readonly IExecutionControlService _executionControlService;
    public YourService(IExecutionControlService executionControlService) {
        _executionControlService = executionControlService;
    }

    public Task Foo(string message) {
        return _executionControlService.StopExecution(new {
            message
        });
    }

    public Task Bar() {
        return _executionControlService.StopExecution(new {
            message = "Something went wrong!"
        });
    }

    // Etc
}

If there is a need for a default implementation, I might create a template.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.1 362 4/3/2025 3.0.1 is deprecated because it is no longer maintained and has critical bugs.
3.0.0 273 3/28/2025 3.0.0 is deprecated because it is no longer maintained and has critical bugs.
2.2.6 599 3/25/2025 2.2.6 is deprecated because it is no longer maintained and has critical bugs.
2.2.5 609 3/25/2025 2.2.5 is deprecated because it is no longer maintained and has critical bugs.
2.2.4 612 3/25/2025 2.2.4 is deprecated because it is no longer maintained and has critical bugs.
2.2.3 608 3/25/2025 2.2.3 is deprecated because it is no longer maintained and has critical bugs.
2.2.2 602 3/24/2025 2.2.2 is deprecated because it is no longer maintained and has critical bugs.
2.2.1 641 3/24/2025 2.2.1 is deprecated because it is no longer maintained and has critical bugs.
2.2.0 565 3/24/2025 2.2.0 is deprecated because it is no longer maintained and has critical bugs.
2.1.1 290 3/23/2025 2.1.1 is deprecated because it is no longer maintained and has critical bugs.
2.0.1 278 3/22/2025 2.0.1 is deprecated because it is no longer maintained and has critical bugs.
2.0.0 288 3/22/2025 2.0.0 is deprecated because it is no longer maintained and has critical bugs.
1.1.0 280 3/17/2025 1.1.0 is deprecated because it is no longer maintained and has critical bugs.
1.0.3 284 3/17/2025 1.0.3 is deprecated because it is no longer maintained and has critical bugs.
1.0.1 276 3/17/2025 1.0.1 is deprecated because it is no longer maintained and has critical bugs.
1.0.0 254 3/17/2025 1.0.0 is deprecated because it is no longer maintained and has critical bugs.