stevejgordon / CorrelationId

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

Check if response header exists before trying to add it #4

Closed stevejgordon closed 6 years ago

stevejgordon commented 6 years ago

This bug could be experienced in two main example cases.

  1. The registration of UseCorrelationId within Startup.Configure is made after the ASP.NET Core exception middleware which re-executes the request pipeline (i.e. UseExceptionHandler). In that case, if the CorrelationIdMiddleware is run twice it will register two delegates with Response.OnStarting which both try to add the correlation ID header to the response (assuming IncludeInResponse is true).

  2. A header already exists on the response from somewhere else in the client application with the same name as the Correlation ID middleware is trying to set. By default this is X-Correlation-ID.

Therefore it makes sense to avoid the risk of exceptions by checking first that the header does not exist before trying to add it to the response.

Fixes #3