azure-webjobs-sdk
azure-webjobs-sdk copied to clipboard
Fix InstanceServices is null on FunctionBindingContext
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.
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.
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;
This was exactly what we needed. Thanks @swoog