0

I'm passing user information as part of each call to a stateful service. I use this information for audit purposes in the service.

Do I have to pass this information around within the service, or is there some other mechanism such as a user context to keep the data in that I can access globally? I have used thread storage (data slots) in the past to hold data, but since the code is async I assume that won't work?

Per
  • 491
  • 6
  • 19

2 Answers2

1

There's a concept called CallContext, that could help. Read more here: http://blog.stephencleary.com/2013/04/implicit-async-context-asynclocal.html

LoekD
  • 11,402
  • 17
  • 27
1

You can't keep that information in some member in the service since you have no control over how concurrent calls are being executed async on multiple threads. The way the runtime allows Tasks and async methods to 'jump' between means that ThreadStorage is not a safe way to keep that context.

What you need is something that can carry all the way from your ServiceRemotingDispatcher down to the execution of your actual service methods and is not affected by concurrent calls (to potentially the same method). The way the underlying Service Fabric implementation executes your method means that there is multiple Tasks and async methods that are being called, this also means that simply based on this the Thread id is out of the question as an option since it is almost guaranteed to jump threads at least once before you reach your code.

I wrote this answer to a similar question (that was deleted, so I added this q back myself). It basically solves your question and it is based on CallContext as mentioned by @LoekD

Community
  • 1
  • 1
yoape
  • 3,285
  • 1
  • 14
  • 27