in src/Microsoft.Azure.WebJobs.Host/Bindings/AttributeCloner.cs [95:141]
private BindingDataResolver GetResolver(PropertyInfo propInfo, INameResolver nameResolver, BindingDataContract contract)
{
// Do the attribute lookups once upfront, and then cache them (via func closures) for subsequent runtime usage.
object originalValue = propInfo.GetValue(_source);
ConnectionStringAttribute connStrAttr = propInfo.GetCustomAttribute<ConnectionStringAttribute>();
AppSettingAttribute appSettingAttr = propInfo.GetCustomAttribute<AppSettingAttribute>();
AutoResolveAttribute autoResolveAttr = propInfo.GetCustomAttribute<AutoResolveAttribute>();
Validator validator = GetValidatorFunc(propInfo, appSettingAttr != null);
if (appSettingAttr == null && autoResolveAttr == null && connStrAttr == null)
{
validator(originalValue);
// No special attributes, treat as literal.
return (newAttr, bindingData) => originalValue;
}
int attrCount = new Attribute[] { connStrAttr, appSettingAttr, autoResolveAttr }.Count(a => a != null);
if (attrCount > 1)
{
throw new InvalidOperationException($"Property '{propInfo.Name}' can only be annotated with one of the types {nameof(AppSettingAttribute)}, {nameof(AutoResolveAttribute)}, and {nameof(ConnectionStringAttribute)}.");
}
// attributes only work on string properties.
if (propInfo.PropertyType != typeof(string))
{
throw new InvalidOperationException($"{nameof(ConnectionStringAttribute)}, {nameof(AutoResolveAttribute)}, or {nameof(AppSettingAttribute)} property '{propInfo.Name}' must be of type string.");
}
var str = (string)originalValue;
// first try to resolve with connection string
if (connStrAttr != null)
{
return GetConfigurationResolver(str, connStrAttr.Default, propInfo, validator, s => _configuration.GetConnectionStringOrSetting(nameResolver.ResolveWholeString(s)));
}
// then app setting
if (appSettingAttr != null)
{
return GetConfigurationResolver(str, appSettingAttr.Default, propInfo, validator, s => _configuration[s]);
}
// Must have an [AutoResolve]
// try to resolve with auto resolve ({...}, %...%)
return GetAutoResolveResolver(str, autoResolveAttr, nameResolver, propInfo, contract, validator);
}