In my asp.net core 3.1 app I have a RequestInfo class that contains information about the current request, and I'd like to make an instance of RequestInfo availale to middleware and controllers by dependency injection. Obviously the RequestInfo must be constructed for every request, and needs the HttpContext to construct it. I'm registering a transient service like this:
services.AddTransient<RequestInfo>(sp =>
{
var context = sp.GetService<HttpContext>();
return new RequestInfo(context)
});
The problem is that when my factory is called, there is no HttpContext in the service provider.
I have tried addScoped, and I've also tried putting services.AddHttpContextAccessor(); in the top of my ConfigureServices and then calling sp.GetService<HttpContextAccessor>().HttpContext in the factory, but neither of those worked.
So what I'm asking is, is there a way to specify that my service factory isn't called until the service provider has already injected the HttpContext.
I realise there are other approaches: I could inject a RequestInfoFactory and then have the consumer call GetRequestInfo on that, or that I could use a middleware to construct the RequestInfo and add it into context.Items. I just want to find out if there's a way to inject the RequestInfo directly because that seems cleaner to me.