# Home

[Vostok.Context](https://github.com/vostok/context) – library for spreading the context across the [asynchronous control flow](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/control-flow-in-async-programs). It also supported spreading the context across the network.

### Spreading the context

When the code is executed, asynchronous control flow is built. Sometimes we want to spread some context at the tree. For example, we want to have to access information about a user or tracing by default.&#x20;

Consider the following situation:\
A user had come in our service. We know something about him. For example, we know his id.\
Control flow looks like this:

&#x20;Handle()\
....Method1()\
........Method2()\
............ –\
................MethodN()

We want to have to access information about the user at every level of asynchronous control flow. But at the same time, we wouldn't need to throw this data explicitly.\
For example,  we want to say: "UserId is xxx now" in `Handle()` and get UserId in `MethodN()` :

&#x20;Handle() <------- Context.Put ("UserId", xxx)\
....Method1()\
........Method2()\
............ –\
................MethodN() <------- Context.Get("UserId")

If the code is running in one thread, we can use thread local storage. Just put local for current thread data in this storage. But what if the code is asynchronous and we still want general context?

Microsoft created a solution  –  [AsyncLocal](https://docs.microsoft.com/en-us/dotnet/api/system.threading.asynclocal-1?view=netframework-4.7.2).&#x20;

### Why do you need [Vostok.Context](https://github.com/vostok/context)?

Context is more user-friendly wrapper above the AsyncLocal. \
The main value of Vostok-Context is integration with other Vostok's libraries. For example, if you use  [Vostok.Context](https://github.com/vostok/context) + [ClusterClient](https://github.com/vostok?utf8=✓\&q=clusterclient\&type=\&language=) , you can automatically spread the context across the network. (Not just in a current asynchronous stream.)

By way of illustration:\
A user has come in our service, and we know his id. But calls go through another service unlike the example above. It looks like this:

Service1 \
Handle()  \
....Method1()                                \
........Method2()\
............ –\
................MethodN()\
&#x20;                          |\
&#x20;                          \|    HTTP request\
&#x20;                          |\
&#x20;                         \\/\
&#x20;               Service2\
&#x20;               Handle()\
&#x20;               ....Method1()                \
&#x20;               ........Method2()\
&#x20;               ............ –\
&#x20;               ................MethodM()

We want to have access to information on the user at every level of asynchronous control flow in the Service2.

You can spread the context across the network with Vostok.Context +  ClusterClient.\
The function of the Context is serialization and deserialization of disseminated data. Use model, connecting ClusterClient and Context – [Vostok.Clusterclient.Context](https://github.com/vostok/clusterclient.context) to send context over the network.

### Interfaces

There are two different interfaces in Context:

| Interface                                                                                                | Description                                                                                                                                                                                                                                   |
| -------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [IContextGlobals](https://github.com/vostok/context/blob/master/Vostok.Context/IContextGlobals.cs)       | <p>Represents mutable type-based ambient context properties. These properties are global in a sense that they can only hold one value of each type.</p><p></p><p>Get context by type:<br><code>FlowingContext.Globals.Get\<int>();</code></p> |
| [IContextProperties](https://github.com/vostok/context/blob/master/Vostok.Context/IContextProperties.cs) | <p>Represents mutable name-based ambient context properties.</p><p></p><p>Get context by key:</p><p><code>FlowingContext.Properties.Get\<long>("a");</code></p>                                                                               |

### When do you need [Vostok.Context](https://github.com/vostok/context)?

The main prime example is trace. \
If you use the Context, you can add prefix to operation like at this example:


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://vostok.gitbook.io/context/master.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
