Search code examples
.net-coreazure-functionsazure-webjobsazure-webjobssdk

What is a BindAsync method with two params in the IBinding interface


In the Azure WebJobs SDK, we have the IBinding interface. This interface has a method BindAsync with two params, but I can't understand what is the first param object value and when this overload method will be called. The same question related ITriggerBinding interface.

I have tried to find the answer in the SDK code source. I know that BindingSource contains a dictionary of parameters where the key is an argument name and value is an argument value that will be provided to the BindAsync method, but I cannot understand what these arguments are and where they come from?


Solution

  • UPDATE

    IBinding.BindAsync Method Returns Task<IValueProvider>.

    The usage of IBinding.BindAsync.

    Microsoft.Azure.WebJobs.Host.Bindings.IBinding.BindAsync(Microsoft.Azure.WebJobs.Host.Bindings.BindingContext)

    Sample Code

    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);
    }
    

    From sample code(HttpTrigger), you will find request, context. They are two params in BindAsync. You will know the usage of BindAsync.