0

I am trying to maintain data between parent and its child threads in .NET Core web applications.

where I need to store the web application name and web request URL of the Parent thread and needs to use it when its child thread starts its execution. Even if the Parent thread completes its execution before its child thread starts its execution i need to maintain the parent data. I have tried Execution Context, Sync Local, Thread Local/static to maintain data between parent and child threads, and did not help.

  1. Can anyone suggest the way/ways to maintain data between parent and its child threads in .NET Core.
  2. In .NET Framework I found CallContext.LogicalSetData() and CallContext.LogicalGetData(), but .NET Core doesn't support Call Context.

Could anyone suggest us an Alternative approach for this in .Net Core?
System.Runtime.Remoting.Messaging ==> class CallContext

Thanks in Advance.

Ziaullah Khan
  • 2,020
  • 17
  • 20
Durai
  • 87
  • 12
  • Does this answer your question? [Alternative for Call Context in .NET Core](https://stackoverflow.com/questions/67110014/alternative-for-call-context-in-net-core) – Peter Bons Apr 16 '21 at 06:44
  • Please refrain from asking the same question twice. Your previous attempt had a nice link to an answer in the comment. – Peter Bons Apr 16 '21 at 06:45

2 Answers2

0

Your question is more of an opinion/approach rather than a technical problem. Here is the best I can answer it.

Inter-Process Communication (IPC) is very tricky.

The idea is that you have a shared object that works in uni/bi-direction.

Consider producer/consumer pattern. Consider Redis cache.

Strong statement alert. Read/apply at your own risk/understanding

  1. I'd suggest moving away from threading. You should only create threads in .NET if you are writing an operating system :) In all the other cases you should use async/await.
  2. Also, try fitting in ConcurrentQueue<T> if that helps solve IPC. They all translate to message-broker pattern.
  3. TaskCompletionSource is another nice strategy to facilitate IPC.
Ziaullah Khan
  • 2,020
  • 17
  • 20
  • 1
    I don't think IPC has a relation to the question. Nothing seems to indicate the OP wants to communicatie between seperate processes, instead OP is looking for communication between threads in the same process. It seems like the OP wants to flow some information from a thread to sub threads, not sure how redis cache, TaskCompletionSource or ConcurrentQueue can help with that, can you elaborate on that? – Peter Bons Apr 16 '21 at 06:48
  • IPC in a broader sense. Two separate entities, trying to exchange data. When disconnected/independent entities communicate they all have the same challenges (be it a process, thread, task or two computers). And the solution generally is created a shared resource (memory, queue or object), establish a synchronization technique (locks, semaphores) and produce/consume. And that’s why I said IPC. Let me put together something that uses TaskCompletionSource. – Ziaullah Khan Apr 16 '21 at 15:08
0

Have you tried using AsyncLocal? In one of my libraries (.NET standard 2.0 which is consumed both by .NET core and .NET Framework) I use this object to do just what you need - share data within the same execution context (also across multiple threads)

In my example below (in my controller - .NET Core 3.1 template whether controller) you can see that even after response reached the client the data was still intact

private static readonly AsyncLocal<string> SharedData = new AsyncLocal<string>();

[HttpGet]
public IEnumerable<WeatherForecast> Get()
{    
    SharedData.Value = "SharedData";
    Console.WriteLine($"Shared data in main thread = {SharedData.Value}");
    // Output: 'Shared data in main thread = SharedData'
    Task.Factory.StartNew(() =>
    {
        Task.Delay(5_000).Wait();
        Console.WriteLine($"Shared data in delayed task = {SharedData.Value}");
        // Output: Shared data in delayed task = SharedData
    });    
    // Additional code...
}
Yaron Y
  • 105
  • 7