[AutoResolve] not working as expected
See original GitHub issueI’m having a hard time reconciling various sources to create a custom Azure Function App binding.
I find examples that bind an attribute and its values to an input argument type using BindToInput()
.
I also find examples that use an IBindingProvider
-implementation using Bind()
.
However, I cannot find samples that use both an IBindingProvider
-implementation while still making use of an attribute.
Specifically, I need my custom binding to access values specified at the attribute level.
Repro steps
I have uploaded a sample custom binding with:
- A custom attribute with an
[AutoResolve]
decorated property.
[Binding]
[AttributeUsage(AttributeTargets.Parameter)]
public class AuthorizeAttribute : Attribute
{
/// <summary>
/// Initialize a new instance of the <see cref="AuthorizeAttribute" /> class.
/// </summary>
/// <param name="applications"></param>
public AuthorizeAttribute(string applications)
{
Applications = applications;
}
/// <summary>
/// The comma-separated list of allowed application identifiers (client-ids).
/// May contain binding parameters.
/// </summary>
[AutoResolve]
public string Applications { get; private set; }
}
- My config provider uses the following code to try and retrieve the attribute:
/// <summary>
/// Creates the <see cref="ClaimsPrincipalBinding" /> class.
/// </summary>
/// <param name="context"></param>
public Task<IBinding> TryCreateAsync(BindingProviderContext context)
{
var parameter = context.Parameter;
var attribute = parameter.GetCustomAttribute<AuthorizeAttribute>(inherit: false);
IBinding binding = new ClaimsPrincipalBinding(attribute, logger_);
return Task.FromResult(binding);
}
When using this custom binding, the attribute
variable is initialized with the value specified from the function’s code.
For instance, instead of resolving the configuration value from app settings, the binding receives values such as %MyConfiguration%
including the percent-characters.
Expected behavior
I would expect the attribute
variable to receive the value referred to by the configuration parameter from app settings.
Actual behavior
The attribute
variable is initialized with the value specified in the source code verbatim, including %-characters.
Summary
Is there any reliable sample that illustrates this problem?
How can I make sure [AutoResolve]
works and resolve configuration parameters from app settings.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (1 by maintainers)
Top GitHub Comments
When implementing the binding yourself by providing your own IBindingProvider, AutoResolve won’t work. Instead, you must use INameResolver to manually resolve the attribute value. An example can be found here. So you’d update your config provider to take the INameResolver from DI and flow it down into your binding provider (example here), and call ResolveInWholeString yourself.
AutoResolve works only if you’re using the BindToInput method, as you’ve found. That method auto creates the binding for you rather than you implementing an IBindingProvider, and that auto binding passes the attribute through an AttributeCloner which is the component that honors AutoResolve.
@mathewc thanks a lot.
That is awesome and informative feedback and looks like exactly what I was looking for. Will let you know if I need more help.