【问题标题】:Azure Function blob bindingAzure 函数 blob 绑定
【发布时间】:2017-08-22 12:24:06
【问题描述】:

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

我得到的错误是:

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:

"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:

[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 配置保留在代码之外,这样我的函数就会变得更加可移植。

【问题讨论】:

  • 您在使用 VS 2017 工具吗?如果是,您能否在输出文件夹中检查一个名为 Harvester 的文件夹,其中包含一个 function.json 文件。你能检查一下它是否有一个名为configurationSource 的属性以及它的值吗?
  • 我正在使用 VS2017 工具,检查了文件,但没有名为 configurationSource 的此类属性。你有没有提到configReader?如果是这样,那就像我原来的问题中描述的那样(顺便说一句,上面的清单来自 azure UI)。
  • 不,configurationSource 是 2017 年工具引入的一个新属性,用于告诉函数运行时使用属性与配置文件。你能在你的csproj中检查``的版本吗?最新的是1.0.2,但请注意这应该将configurationSource 设置为attributes,您需要将其更改为config 以告诉运行时使用配置而不是属性。然后你需要在你的项目中包含那个 json 文件
  • 你的问题是关于Microsoft.NET.Sdk.Functions 包,事实上我在1.0.0-alpha3。切换到1.0.2 并渲染configurationSrouce。感谢那!如果我在项目中包含 JSON 文件,它是否需要指向我的输出文件夹中的位置 (bin\Debug...)?我已将它作为文件包含在我的源代码树中,但它仍会生成 configurationSource: 'attributes'
  • 感谢分享,我可以确认它按预期工作。请升级您的评论作为答案,我会这样反馈。谢谢!

标签: c# azure azure-functions


【解决方案1】:

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

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

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

现在您可以将函数定义为

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

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

{
  "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 的值。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    相关资源
    最近更新 更多