Azure 函数 blob 绑定

Azure Function blob binding(Azure 函数 blob 绑定)

本文介绍了Azure 函数 blob 绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果不使用 C# 实现(不是 CSX)中的 [BlobAttribute],我无法将 blob 类型的输入参数绑定到字符串/TextReader.

I'm not able to bind an input parameter of type blob to either string/TextReader without using the [BlobAttribute] in a C# implementation (not CSX).

我得到的错误是:

Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.Harvester'. 
Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'configReader' to type 
TextReader. Make sure the parameter Type is supported by the binding. If 
you're using binding extensions (e.g. ServiceBus, Timers, etc.) make sure 
you've called the registration method for the extension(s) in your startup 
code (e.g. config.UseServiceBus(), config.UseTimers(), etc.).

function.config:

function.config:

"bindings": [
    {
      "type": "timerTrigger",
      "schedule": "0 */5 * * * *",
      "useMonitor": true,
      "runOnStartup": false,
      "direction": "in",
      "name": "myTimer"
    },
    {
      "type": "blob",
      "name": "configReader",
      "path": "secured/app.config.json",
      "connection": "XXX",
      "direction": "in"
    }
  ],

函数签名(不绑定configReader):

[FunctionName("Harvester")]
 public static async Task Run(
   [TimerTrigger("0 */5 * * * *")]TimerInfo myTimer,
   TraceWriter log,
   TextReader configReader)

尽管如此(BINDING configReader:

This would work though (BINDING configReader:

[FunctionName("Harvester")]
 public static async Task Run(
   [TimerTrigger("0 */5 * * * *")]TimerInfo myTimer,
   TraceWriter log,
   [Blob("secured/app.config.json", FileAccess.Read)]TextReader configReader)

关于如何在不指定 BlobAttribute 中的 blob 路径的情况下使其工作的任何想法.理想情况下,我会将 Blob 配置保留在代码之外,这样我的函数就会变得更加可移植.

Any idea on how to get it working without specifying the blob path in BlobAttribute. I'd ideally keep the Blob config outside of the code, that way my function would become more portable.

推荐答案

问题出在最新的运行时支持 function.json 中的新属性 (configurationSource)代码>

The issue turned out to be with the latest runtime supporting a new property (configurationSource) in function.json

这告诉运行时使用 config(即 function.json)或 C# 属性进行函数配置.

This tells the runtime to use either config (which is function.json) or C# attributes for function config.

基本上允许您像这样定义您的函数

essentially allowing you to either define your function like this

现在您可以将函数定义为

Now you can either define your function as

[FunctionName("Harvester")]
public static async Task Run(
    [TimerTrigger]TimerInfo myTimer,
    TraceWriter log,
    TextReader configReader)
{
}

还有一个看起来像这样的 function.json

along with a function.json that looks like this

{
  "generatedBy": "Microsoft.NET.Sdk.Functions-1.0.0.0",
  "configurationSource": "config",
  "bindings": [
    {
      "type": "timerTrigger",
      "schedule": "0 */5 * * * *",
      "useMonitor": true,
      "runOnStartup": false,
      "direction": "in",
      "name": "myTimer"
    },
    {
      "type": "blob",
      "name": "configReader",
      "path": "secured/app.config.json",
      "connection": "XXX",
      "direction": "in"
    }
  ],
  "disabled": false,
  "scriptFile": "...",
  "entryPoint": "..."
}

或者像这样

[FunctionName("Harvester")]
public static async Task Run(
    [TimerTrigger("0 */5 * * * *")]TimerInfo myTimer,
    TraceWriter log,
    [Blob("secured/app.config.json", FileAccess.Read)]TextReader configReader)
{
}

像这样更简单的配置

{
  "generatedBy": "Microsoft.NET.Sdk.Functions-1.0.0.0",
  "configurationSource": "attributes",
  "bindings": [
    {
      "type": "timerTrigger",
      "name": "myTimer"
    },
  ],
  "scriptFile": "...",
  "entryPoint": "..."
}

注意两个示例中 configurationSource 的值.

note the value of configurationSource in both examples.

Visual Studio 2017 的工具默认执行后者.如果您想更改您的 function.json 以包含您的所有配置并更改 configurationSource 您将需要在您的项目中包含该文件并将其标记为始终复制.这个 GIF 展示了如何做到这一点.

The tooling for Visual Studio 2017 does the latter by default. If you wanna change your function.json to include all your config and change the configurationSource you will need to include the file in your project and mark it as always copy. This GIF shows how to do that.

这篇关于Azure 函数 blob 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Azure 函数 blob 绑定

基础教程推荐