azure-webjobs-sdk icon indicating copy to clipboard operation
azure-webjobs-sdk copied to clipboard

Fix InstanceServices is null on FunctionBindingContext

Open swoog opened this issue 4 years ago • 3 comments

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.

swoog avatar Dec 29 '21 15:12 swoog

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.

gorillapower avatar Mar 18 '22 14:03 gorillapower

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;

swoog avatar Mar 24 '22 14:03 swoog

This was exactly what we needed. Thanks @swoog

eOkadas avatar Mar 24 '22 14:03 eOkadas