简体   繁体   中英

Azure Function blob binding

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).

The error I'm getting is:

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"
    }
  ],

Function signature (NOT BINDING configReader ):

[FunctionName("Harvester")]
 public static async Task Run(
   [TimerTrigger("0 */5 * * * *")]TimerInfo myTimer,
   TraceWriter log,
   TextReader 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)

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.

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

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

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

or like this

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

with a simpler config like this

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

note the value of configurationSource in both examples.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM