Open dolphinspired opened 3 years ago
@fabiocav , is this possible?
is there some other way to access the FunctionContext through dependency injection?
Thanks!
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.
@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 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.
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.
great work
@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?
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.
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 toIHttpContextAccessor
, 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!