azure-function-dependency-injection
azure-function-dependency-injection copied to clipboard
Inject HttpRequest
I have services that depend on IHttpContextAccessor
so that certain HTTP Headers and claim data can be accessed. Is there any way to configure DI for some form of the current HttpRequest?
What I use is:
public IServiceProvider Build()
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
But currently, Startup seems to be not invoked with .net core 2.2 ...
What I use is:
public IServiceProvider Build() { services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); }
But currently, Startup seems to be not invoked with .net core 2.2 ...
Yeah, that works great for a regular dotnet core web project. It does not work for Azure Functions. The HttpContext
property is always null. Perhaps I'm doing something incorrectly.
Did you include an [assembly: FunctionsStartup(typeof(MyStartupClass))] attribute somewhere in your function?
i also would like to be able to inject the httpRequest into a service, e.g. token validation etc.
Hi,
i cannot see your post… Which version of which library is now supporting this ?
//nick
Am 18.12.2020 um 00:33 schrieb Ed Downs [email protected]:
This is pretty old, but I needed to do this as well. With the newer injection system, in your Startup class, you can use:
public override void Configure(IFunctionsHostBuilder builder) { builder.Services.AddHttpContextAccessor(); }
Then simply inject IHttpContextAccessor where you need it. Seems to work just fine.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/BorisWilhelms/azure-function-dependency-injection/issues/32#issuecomment-747771722, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACL3U64YTEWROQRJT63G3BTSVKIM3ANCNFSM4GVAGHTQ.