Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix InstanceServices is null on FunctionBindingContext #2814

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

swoog
Copy link

@swoog swoog commented Dec 29, 2021

I have used FunctionBindingContext with dependency injection.
But I cannot access to the IServiceProvider scoped to the Function instance. The property InstanceServices is null. I think this is a mistake. I do not see a set of this property.

I have test to use _functionInvocationServices directly in my code and the behavior seems to be good.

@gorillapower
Copy link

Will this be merged in the next release? Is there a work around for now? I need to access some injected services from my Durable Entity static Run() method.

@swoog
Copy link
Author

swoog commented Mar 24, 2022

Actual workaround I used :

var fieldInfo = context.FunctionContext.GetType().GetField("_functionInvocationServices", BindingFlags.Instance | BindingFlags.NonPublic);

if (fieldInfo == null)
{
    throw new ArgumentException("Not found _functionInvocationServices in the Azure Function SDK on FunctionContext (Hack not working)");
}

var serviceProvider = fieldInfo.GetValue(context.FunctionContext) as IServiceProvider;

@eOkadas
Copy link

eOkadas commented Mar 24, 2022

This was exactly what we needed. Thanks @swoog

@@ -89,7 +89,7 @@ public FunctionBindingContext(IFunctionInstanceEx functionInstance, Cancellation
/// <summary>
/// The service provider for the current function invocation scope.
/// </summary>
public IServiceProvider InstanceServices { get; set; }
public IServiceProvider InstanceServices => this._functionInvocationServices;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, looks like this issue has existed since this property was originally added in f7da27d.

Please add a test to the existing FunctionBindingContext test suite that tests this property and potentially others as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants