We use dependency injection in our Azure Function (v2 on netstandard20) using parameter binding with IExtensionConfigProvider. After upgrading Microsoft.NET.Sdk.Functions from 1.0.13 to 1.0.19 (which forced an upgrade of Microsoft.Azure.Webjobs.Host to v3) this doesn't work anymore. I can't hit a breakpoint in my IExtensionConfigProvider.Initialize function any more. The same version of the Functions SDK works fine for a sample project with target framework net462, for which it uses Microsoft.Azure.WebJobs v2.
Here's the error it gives at runtime:
Error indexing method 'Function1.Run'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'customThing' to type CustomType. Make sure the parameter Type is supported by the binding.
And here's the code for the sample app:
public static class Function1
{
[FunctionName("ThisFunction")]
public static async Task Run(
[TimerTrigger("0 */1 * * * *")]TimerInfo timer,
[Inject(typeof(CustomType))] CustomType customThing,
ExecutionContext context)
{
Console.WriteLine(customThing.GetMessage());
}
}
public class CustomType
{
public string GetMessage() => "Hi";
}
[Binding]
[AttributeUsage(AttributeTargets.Parameter)]
public class InjectAttribute : Attribute
{
public Type Type { get; }
public InjectAttribute(Type type) => Type = type;
}
public class InjectConfiguration : IExtensionConfigProvider
{
private IServiceProvider _serviceProvider;
public void Initialize(ExtensionConfigContext context)
{
var services = new ServiceCollection();
services.AddSingleton<CustomType>();
_serviceProvider = services.BuildServiceProvider(true);
context
.AddBindingRule<InjectAttribute>()
.BindToInput<dynamic>(i => _serviceProvider.GetRequiredService(i.Type));
}
}