SnapExit 2.2.1
This package was discontinued due to limitations in Task programming causing it to produce many memory leaks.
See the version list below for details.
dotnet add package SnapExit --version 2.2.1
NuGet\Install-Package SnapExit -Version 2.2.1
<PackageReference Include="SnapExit" Version="2.2.1" />
<PackageVersion Include="SnapExit" Version="2.2.1" />
<PackageReference Include="SnapExit" />
paket add SnapExit --version 2.2.1
#r "nuget: SnapExit, 2.2.1"
#:package SnapExit@2.2.1
#addin nuget:?package=SnapExit&version=2.2.1
#tool nuget:?package=SnapExit&version=2.2.1
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 | Versions 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. |
-
net8.0
-
net9.0
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.0 | 273 | 3/28/2025 | |
| 2.2.6 | 599 | 3/25/2025 | |
| 2.2.5 | 609 | 3/25/2025 | |
| 2.2.4 | 612 | 3/25/2025 | |
| 2.2.3 | 608 | 3/25/2025 | |
| 2.2.2 | 602 | 3/24/2025 | |
| 2.2.1 | 641 | 3/24/2025 | |
| 2.2.0 | 565 | 3/24/2025 | |
| 2.1.1 | 290 | 3/23/2025 | |
| 2.0.1 | 278 | 3/22/2025 | |
| 2.0.0 | 288 | 3/22/2025 | |
| 1.1.0 | 280 | 3/17/2025 | |
| 1.0.3 | 284 | 3/17/2025 | |
| 1.0.1 | 276 | 3/17/2025 | |
| 1.0.0 | 254 | 3/17/2025 |