title: How to access/pass data from different scope
body: <p>For some reason I need to access data from different scope that is set in another scope. Please advise how this can be achieved. Here is the same code where I set CallContext value in one scope and would like to get same value from newly created scope.</p>
  <pre><code>    public class CallContext
      {
          ConcurrentDictionary&lt;string, AsyncLocal&lt;object&gt;&gt; state = new ConcurrentDictionary&lt;string, AsyncLocal&lt;object&gt;&gt;();
  
          public void SetData(string name, object data) =&gt;
              state.GetOrAdd(name, _ =&gt; new AsyncLocal&lt;object&gt;()).Value = data;
  
          public object GetData(string name) =&gt;
              state.TryGetValue(name, out AsyncLocal&lt;object&gt; data) ? data.Value : null;
      }
  
  
      public interface ICustomerService
      {
          void Log();
      }
  
  
      public class CustomerService : ICustomerService
      {
          private CallContext _callContext;
          private static IHttpContextAccessor _httpContextAccessor;
          private IServiceScopeFactory _serviceScopeFactory;
          public CustomerService(CallContext callContext, IHttpContextAccessor httpContextAccessor, IServiceScopeFactory serviceScopeFactory)
          {
              _callContext = callContext;
              _httpContextAccessor = httpContextAccessor;
              _serviceScopeFactory = serviceScopeFactory;
          }
  
          public void Log()
          {
              var context = _httpContextAccessor.HttpContext;
  
              _callContext.SetData(&quot;ThreadId&quot;, context.TraceIdentifier);
              _callContext.SetData(&quot;URL&quot;, context?.Request?.Scheme + &quot;://&quot; + context?.Request?.Host.Value + context?.Request?.Path.Value);
  
              // For some reason I need to create a new scope
              using (var scope = _serviceScopeFactory.CreateScope())
              {
                  var customerPortalService = scope.ServiceProvider.GetRequiredService&lt;ISecondCustomerService&gt;();
  
                  customerPortalService.Log();
              }
          }
      }
  
  
      public interface ISecondCustomerService
      {
          void Log();
      }
  
  
      public class SecondCustomerService : ISecondCustomerService
      {
          private CallContext _callContext;
          public SecondCustomerService(CallContext callContext)
          {
              _callContext = callContext;
          }
          public void Log()
          {
              var threadId = _callContext.GetData(&quot;ThreadId&quot;);
              var url = _callContext.GetData(&quot;URL&quot;);
          }
      }
  
  
  
      public class Startup
      {
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;
          }
  
          public IConfiguration Configuration { get; }
  
          // This method gets called by the runtime. Use this method to add services to the container.
          public void ConfigureServices(IServiceCollection services)
          {
              services.AddControllers();
  
              services.AddTransient&lt;IHttpContextAccessor, HttpContextAccessor&gt;();
              services.AddScoped&lt;CallContext&gt;();
              services.AddScoped&lt;ICustomerService, CustomerService&gt;();
              services.AddScoped&lt;ISecondCustomerService, SecondCustomerService&gt;();
          }
  
          // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
          public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
  
              app.UseHttpsRedirection();
  
              app.UseRouting();
  
              app.UseAuthorization();
  
              app.UseEndpoints(endpoints =&gt;
              {
                  endpoints.MapControllers();
              });
          }
      }
  
  
      [ApiController]
      [Route(&quot;[controller]&quot;)]
      public class WeatherForecastController : ControllerBase
      {
          private readonly ICustomerService customerService;
          public WeatherForecastController(ICustomerService _customerService)
          {
              customerService = _customerService;
          }
  
          private static readonly string[] Summaries = new[]
          {
              &quot;Freezing&quot;, &quot;Bracing&quot;, &quot;Chilly&quot;, &quot;Cool&quot;, &quot;Mild&quot;, &quot;Warm&quot;, &quot;Balmy&quot;, &quot;Hot&quot;, &quot;Sweltering&quot;, &quot;Scorching&quot;
          };
  
          private readonly ILogger&lt;WeatherForecastController&gt; _logger;
  
          public ActionResult Log()
          {
              customerService.Log();
              return Ok();
          }
      }
  </code></pre>
  <p>In above code, I am setting ThreadID and URL value in CallContext from CustomerSerive and would like to get the same value from SecondCustomerService which is within a new scope created in CustomerService.</p>