azure-function-dependency-injection icon indicating copy to clipboard operation
azure-function-dependency-injection copied to clipboard

Inject HttpRequest

Open tomfaltesek opened this issue 6 years ago • 5 comments

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?

tomfaltesek avatar Feb 07 '19 18:02 tomfaltesek

What I use is:

        public IServiceProvider Build()
        {
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        }

But currently, Startup seems to be not invoked with .net core 2.2 ...

ranouf avatar Feb 07 '19 22:02 ranouf

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.

tomfaltesek avatar Feb 08 '19 14:02 tomfaltesek

Did you include an [assembly: FunctionsStartup(typeof(MyStartupClass))] attribute somewhere in your function?

DonFrazier-zz avatar Sep 13 '19 22:09 DonFrazier-zz

i also would like to be able to inject the httpRequest into a service, e.g. token validation etc.

nickwinger avatar May 26 '20 06:05 nickwinger

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.

nickwinger avatar Dec 18 '20 01:12 nickwinger