stevejgordon / CorrelationId

An ASP.NET Core middleware component which synchronises a correlation ID for cross API request logging.
MIT License
558 stars 106 forks source link

CorrelationContextAccessor should be scoped #14

Closed MovGP0 closed 6 years ago

MovGP0 commented 6 years ago

since CorrelationContextAccessor returns the correlation context of an request, it is probably a bad idea to have it as singleton, because parallel requests will overwrite the CorrelationContext.

serviceCollection.TryAddSingleton<ICorrelationContextAccessor, CorrelationContextAccessor>();

I guess it should be injected per-request instead:

serviceCollection.TryAddScoped<ICorrelationContextAccessor, CorrelationContextAccessor>();
stevejgordon commented 6 years ago

Hi @MovGP0. This is by design since it needs to be (potentially) consumable from Singleton services. I have followed the patterns used by Microsoft for the HttpContextAccessor. The returned CorrelationId should be correct per scoped request despite the accessor being Singleton.

Are you seeing any issues with this when you are using it?

stevejgordon commented 6 years ago

Marking as closed.