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

查看:26
本文介绍了如何使用 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/等)您也可以反复进行多次绑定.

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(在本例中为 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天全站免登陆