This bug could be experienced in two main example cases.
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).
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.
This bug could be experienced in two main example cases.
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).
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