I have a web API developed using the ASP.NET API. Each incoming request has an inserted custom header value. for example x-correlationid. The controller uses this value to register and track the request. I am currently reading the value in each controller as shown below
[Route("api/[controller]")]
public class DocumentController : Controller
{
private ILogger<TransformController> _logger;
private string _correlationid = null;
public DocumentController(ILogger<DocumentController > logger)
{
_logger = logger;
_correlationid = HttpContext.Request.Headers["x-correlationid"];
}
[HttpPost]
public async Task<intTransform([FromBody]RequestWrapper request)
{
_logger.LogInformation("Start task. CorrelationId:{0}", _correlationid);
_logger.LogInformation("End task. CorrelationId:{0}", _correlationid);
return result;
}
}
I think this is against the rules of DI.
Instead of reading the value inside the controller constructor, I want to enter the value into the controller constructor.
Or
Can middleware read x-correlationidand *somehow*make it available to all controllers, so we donβt need to enter it into any controller?
What would be better here?