问题描述
在 Azure WebJobs SDK 中,我们有 IBinding
接口。这个接口有一个方法 BindAsync
有两个参数,但我不明白第一个参数 object value
是什么以及这个重载方法什么时候被调用。
同样的问题与 ITriggerBinding
接口相关。
我试图在 SDK 代码源中找到答案。我知道 BindingSource
包含一个参数字典,其中键是参数名称,值是将提供给 BindAsync
方法的参数值,但我无法理解这些参数是什么以及它们在哪里从哪里来?
解决方法
更新
IBinding.BindAsync 方法 返回 Task<IValueProvider>
。
IBinding.BindAsync 的用法。
示例代码
public async Task BindAsync_String()
{
ParameterInfo parameterInfo = GetType().GetMethod("TestStringFunction").GetParameters()[0];
HttpTriggerAttributeBindingProvider.HttpTriggerBinding binding = new HttpTriggerAttributeBindingProvider.HttpTriggerBinding(new HttpTriggerAttribute(),parameterInfo,false);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,"http://functions/myfunc?code=abc123");
request.Content = new StringContent("This is a test");
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/text");
FunctionBindingContext functionContext = new FunctionBindingContext(Guid.NewGuid(),CancellationToken.None,new TestTraceWriter(TraceLevel.Verbose));
ValueBindingContext context = new ValueBindingContext(functionContext,CancellationToken.None);
ITriggerData triggerData = await binding.BindAsync(request,context);
Assert.Equal(0,triggerData.BindingData.Count);
string result = (string)triggerData.ValueProvider.GetValue();
Assert.Equal("This is a test",result);
}
从示例代码 (HttpTrigger) 中,您会发现 request
、context
。它们是 BindAsync
中的两个参数。您将了解 BindAsync
的用法。