title: How to share data between different threads which belong to different classes?
body: <p>I was reading about async/await recently and I would like to know how to share data between different threads which belong to different classes? Let's assume that we have the <code>HttpContext</code> in some web application. This context contains information about <code>userId</code>, <code>sessionId</code> and so on. Our web application gives some data which is used by some console application which executed on another computer. If errors are occurred in this console application I will write it to a log file. <code>userId</code> and <code>sessionId</code> also should written to this log file. But each thread created in this console application has its own context. So, I'm looking for a way to set <code>userId</code> and <code>sessionId</code> to thread context. I don't want to use some static classes or <code>volatile</code> fields. I put a simple example of my console application below.</p>
  
  <pre><code>    public sealed class MainService
      {
          /// &lt;summary&gt;
          /// The main method which is called.
          /// &lt;/summary&gt;
          public void Execute()
          {
              try
              {
                  searchService.ExecuteSearchAsync().Wait();
              }
              catch (Exception e)
              {
                  // gets additional info (userId, sessionId) from the thread context
                  StaticLoggerClass.LogError(e);
              }
          }
      }
  
      public sealed class SearchService
      {
          private IRepository  repository = new Repository();
  
          public async Task ExecuteSearchAsync()
          {
              try
              {
                  var result = await this.GetResultsAsync();
              }
              catch (Exception e)
              {
                  // gets additional info (userId, sessionId) from the thread context
                  StaticLoggerClass.LogError(e);
              }
          }
  
          private async Task&lt;ResultModel&gt; GetResultsAsync()
          {
              var result = this.repository.GetAsync();
          }
      }
  
      public sealed class Repository
      {
          private IClient client;
  
          public async Task&lt;Entity&gt; GetAsync()
          {
              return await client.GetResultAsync();
          }
      }
  </code></pre>