AsyncLocalRegion 2.1.0.25

dotnet add package AsyncLocalRegion --version 2.1.0.25                
NuGet\Install-Package AsyncLocalRegion -Version 2.1.0.25                
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="AsyncLocalRegion" Version="2.1.0.25" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AsyncLocalRegion --version 2.1.0.25                
#r "nuget: AsyncLocalRegion, 2.1.0.25"                
#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.
// Install AsyncLocalRegion as a Cake Addin
#addin nuget:?package=AsyncLocalRegion&version=2.1.0.25

// Install AsyncLocalRegion as a Cake Tool
#tool nuget:?package=AsyncLocalRegion&version=2.1.0.25                

Async Local Region

Control the lifetime of AsyncLocal<T> objects using the IDisposable pattern. This package enables scoped lifespan management of AsyncLocal values within asynchronous code blocks to ensure values are accessible only during the defined region.

Features

  • Define a scoped region for an AsyncLocal<T> value.
  • Retrieve the value in any inner tasks or method calls.
  • Automatically dispose of the scoped region to prevent accidental value leakage.

Usage

Here's an example demonstrating how to define a region for an AsyncLocal<string> and retrieve it within an asynchronous method:

private static AsyncLocalRegion<string> MyParameter = new();

public static async void Run()
{
    using (MyParameter.StartRegion("Value for this Task only"))
    {
        await RetrieveValue();
    }
}

private static async Task RetrieveValue()
{
    // prints "Value for this Task only"
    Console.WriteLine(MyParameter.CurrentValue); 
}

Installation

dotnet add package AsyncLocalRegion

Use Cases

Passing Data Through Unowned Layers of Code:

  • Serialization Hooks: Use AsyncLocalRegion to pass additional context to the [OnSerialize] method in serialization processes. This is particularly useful in scenarios where serializers do not natively support passing custom data. For example, you can store a user's permission level in AsyncLocalRegion before serialization begins, and access this data within the OnSerialize method to decide which properties of an object should be serialized based on the user's permissions.

    private static AsyncLocalRegion<string> UserPermission = new();
    
    public void PrepareSerializationForAdmin()
    {
        using (UserPermission.StartRegion("Admin"))
        {
            SerializeData();
        }
    }
    
    [OnSerialize]
    private void OnSerialize()
    {
        if (UserPermission.CurrentValue == "Admin")
        {
            // Serialize sensitive data
        }
    }
    
  • Data Mapping: When mapping data from one object to another, AsyncLocalRegion can be used to provide context that guides how data should be mapped, especially useful in libraries where method signatures cannot be altered. For instance, you might need to adapt data differently based on the region or locale, which could be stored in AsyncLocalRegion and accessed during the mapping process.

    private static AsyncLocalRegion<string> MappingContext = new();
    
    public void ConfigureMappingForEU()
    {
      using (MappingContext.StartRegion("EU"))
      {
        MapData();
      }
    }
    
    private void MapData()
    {
      if (MappingContext.CurrentValue == "EU")
      {
        // Map data according to European standards
      }
    }
    
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

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
2.1.0.25 130 4/30/2024
2.1.0.23 1,244 4/28/2023
2.1.0.21 215 4/26/2023
2.1.0.19 265 2/27/2023
2.1.0.13 653 5/31/2022
2.1.0.12 308 5/31/2022