Azure / azure-functions-dotnet-worker

Azure Functions out-of-process .NET language worker
MIT License
427 stars 182 forks source link

FunctionContext is not accessible through dependency injection #574

Open dolphinspired opened 3 years ago

dolphinspired commented 3 years ago

With the introduction of the FunctionContext in the dotnet-isolated (.NET 5) runtime environment for Azure Functions, we now have an easy way to access common Function information and share arbitrary data through the Items property on that context. Unfortunately, although you can access the FunctionContext through middleware and as a direct Function parameter, it is not currently available through dependency injection.

Exception: System.InvalidOperationException: Unable to resolve service for type 'Microsoft.Azure.Functions.Worker.FunctionContext'...

So, two things:

First, a question: is there some other way to access the FunctionContext through dependency injection? I've poked around the code and documentation and haven't found anything yet. Can any of the default injected services access it?

Second, a request: could we have a specific service added by default to access the FunctionContext through DI? Perhaps an IFunctionContextAccessor that would work very similarly to IHttpContextAccessor, which would return the FunctionContext for the current invocation.

In the meantime, I'm attempting to implement my own version of this by grabbing the FunctionContext in a custom middleware and setting it to an AsyncLocal, which can then be referenced by injected services. If it works, I'll come back and post an example here.

UPDATE: Here's my example of an implementation of IFunctionContextAccessor, which you can start using in your dotnet-isolated Functions today!

everettcomstock commented 3 years ago

@fabiocav , is this possible?

is there some other way to access the FunctionContext through dependency injection?

Thanks!

david-peden-q2 commented 3 years ago

I asked a nearly identical question in https://github.com/Azure/azure-functions-dotnet-worker/issues/436 and it has been ignored ever since the bot closed it.

fabiocav commented 3 years ago

@david-peden-q2 apologies if your question went unanswered.

Short answer is, no. The FunctionContext flows with the invocation and is not available through DI, though, this is something we should probably consider.

I'll flag this for further triage and discussion.

david-peden-q2 commented 3 years ago

@fabiocav thank you for the response. https://github.com/Azure/azure-functions-dotnet-worker/issues/572, although misguided in asking for HttpContext, appears to want the same thing.

dolphinspired commented 3 years ago

If you need a quick solution for this, I've created a brief tutorial for how to implement an IFunctionContextAccessor. It uses a singleton accessor with an AsyncLocal value holder (much like AspNetCore's HttpContextAccessor) in combination with a middleware to set the Function context on each invocation.

proforce2k commented 9 months ago

great work

udlose commented 7 months ago

@david-peden-q2 apologies if your question went unanswered.

Short answer is, no. The FunctionContext flows with the invocation and is not available through DI, though, this is something we should probably consider.

I'll flag this for further triage and discussion.

@fabiocav Yes please. Any updates on when this might be added?