如何使用Binder在C#函数中执行动态绑定? [英] How do I use Binder to perform dynamic bindings in my C# Function?

查看:109
本文介绍了如何使用Binder在C#函数中执行动态绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要绑定到输出Blob,但是Blob路径需要在我的函数中动态计算.我该怎么办?

I need to bind to an output blob, but the blob path needs to be computed dynamically in my function. How do I do it?

推荐答案

Binder是一种高级绑定技术,可让您在代码中强制性地执行绑定,而不是声明性地(通过function.json元数据文件).如果绑定路径或其他输入的计算需要在函数的运行时进行,则可能需要执行此操作.请注意,使用Binder参数时,您不应function.json中包含该参数的相应条目.

Binder is an advanced binding technique that allows you to perform bindings imperatively in your code as opposed to declaratively via the function.json metadata file. You might need to do this in cases where the computation of binding path or other inputs needs to happen at runtime in your function. Note that when using an Binder parameter, you should not include a corresponding entry in function.json for that parameter.

在下面的示例中,我们将动态绑定到Blob输出.如您所见,由于您是在代码中声明绑定,因此可以按照您希望的任何方式来计算路径信息.请注意,您还可以绑定到任何其他原始绑定属性(例如QueueAttribute/EventHubAttribute/ServiceBusAttribute/etc.).也可以迭代地绑定多次.

In the below example, we're dynamically binding to a blob output. As you can see, because you're declaring the binding in code, your path info can be computed in any way you wish. Note that you can bind to any of the other raw binding attributes as well (e.g. QueueAttribute/EventHubAttribute/ServiceBusAttribute/etc.) You can also do so iteratively to bind multiple times.

请注意,传递给BindAsync的type参数(在本例中为TextWriter)必须是目标绑定支持的类​​型.

Note that the type parameter passed to BindAsync (in this case TextWriter) must be a type that the target binding supports.

using System;
using System.Net;
using Microsoft.Azure.WebJobs;

public static async Task<HttpResponseMessage> Run(
        HttpRequestMessage req, Binder binder, TraceWriter log)
{
    log.Verbose($"C# HTTP function processed RequestUri={req.RequestUri}");

    // determine the path at runtime in any way you choose
    string path = "samples-output/path";

    using (var writer = await binder.BindAsync<TextWriter>(new BlobAttribute(path)))
    {
        writer.Write("Hello World!!");
    }

    return new HttpResponseMessage(HttpStatusCode.OK); 
}

这是对应的元数据:

{
  "bindings": [
    {
      "name": "req",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "name": "res",
      "type": "http",
      "direction": "out"
    }
  ]
}

有些绑定重载带有属性的数组.如果需要控制目标存储帐户,则传入一组属性,这些属性从绑定类型属性(例如BlobAttribute)开始,然后包含指向要使用的帐户的StorageAccountAttribute实例.例如:

There are bind overloads that take an array of attributes. In cases where you need to control the target storage account, you pass in a collection of attributes, starting with the binding type attribute (e.g. BlobAttribute) and inlcuding a StorageAccountAttribute instance pointing to the account to use. For example:

var attributes = new Attribute[]
{
    new BlobAttribute(path),
    new StorageAccountAttribute("MyStorageAccount")
};
using (var writer = await binder.BindAsync<TextWriter>(attributes))
{
    writer.Write("Hello World!");
}

这篇关于如何使用Binder在C#函数中执行动态绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆