c# - Web API - Set each thread with the HttpRequestMessage id? -
i have web api coded in c#.
the web api uses functionality shared other in-house components. depends on single threaded flows , uses thread local storage store objects, , session information. please don't if it's or bad, that's have deal with.
in web api i've implemented custom message handler (delagatinghandler) sendasync
protected async override system.threading.tasks.task<httpresponsemessage> sendasync(httprequestmessage request, system.threading.cancellationtoken cancellationtoken) which based on tpl , switches threads, , when happens, thread based functionality gets messed up, since i'm losing thread context , data assigned it.
my idea uniquely identify httprequestmessage, think using correlation id should sufficient it
var requestid = request.getcorrelationid(); but want store correlation id of httprequestmessage per each thread allocated in task.
so question if can identify thread that's being allocated under specific task , allocate id it?
for context related problems, can use callcontext.logicalsetdata , callcontext.logicalgetdata, merely idictionary<string, object> flows between contexts, , has copy-on-write (shallow copy) semantics.
since data immutable (according docs), can map correlation id threads managed thread id:
protected async override task<httpresponsemessage> sendasync( httprequestmessage request, cancellationtoken cancellationtoken) { var correlationid = request.getcorrelationid(); var threadid = thread.currentthread.managedthreadid; callcontext.logicalsetdata(correlationid.tostring(), threadid); } and later retrieve if make sure you're on "valid thread".
a read on call context async-await can found here.
Comments
Post a Comment