Azure Function Blob绑定 [英] Azure Function blob binding
问题描述
在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)
尽管如此(绑定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
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
{
"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 Function Blob绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!