Quick Start

Dependencies: .NETStandart 2.0

PM> Install-Package Vostok.Context -Version 0.1.1

SetUp

Let's look at one case. We want to create some threads and make the general context for all of them.

Step 1: Include Vostok.Context library in project:

using Vostok.Context;

Step 2: Create FlowingContext in the Main void:

 static void Main(string[] args)
{
    FlowingContext.Properties.Set("data", "The same context");
}

Step 3: Create DisplayData . This void writes thread's number and data from FlowingContext to the Console:

static void DisplayData()
{
    var data = FlowingContext.Properties.Current["data"];
    var id = Thread.CurrentThread.ManagedThreadId;
    Console.WriteLine($"{data}, thread = {id}");
}

Step 4: Additionally, write the async task Work. With it process some threads:

static async Task Work()
{
    DisplayData();
    await Task.Run(() => DisplayData());
}

Step 5: Call the Work from Main:

 static void Main(string[] args)
{
    FlowingContext.Properties.Set("data", "The same context");

    Work();
    Thread.Sleep(100);
}

Result:

The same context, thread = 1
The same context, thread = 3

Even though the threads were different, context was same.

First usage

Last updated