1
votes

We are creating range of dotnet core 2.0 microservices based on the servicestack framework. We want to use http-header based correlation tokens, so we can track a request in our distributed logging system (Seq).

We would like to use IoC to setup a a class holding a threadsafe JsonServiceClient for performance reasons, but how can we ensure that headers placed on one thread will not leak into another concurrent request? Client code example:

public TResponse Get(IReturn requestDto)

...

_serviceClient.AddHeader("r-id", theReqId); // how can we make these specific for the thread request only?

var responseFromDownstreamService = _serviceClient.Get(requestDto);

1

1 Answers

1
votes

If you’re modifying the service client instance the dependency needs to be transient so each thread receives a new instance they can mutate without modifying the same instance used by other threads.