C# Azure函数blob绑定
如果不在C#实现(不是CSX)中使用[BlobatAttribute],我就无法将blob类型的输入参数绑定到string/TextReader 我得到的错误是:C# Azure函数blob绑定,c#,azure,azure-functions,C#,Azure,Azure Functions,如果不在C#实现(不是CSX)中使用[BlobatAttribute],我就无法将blob类型的输入参数绑定到string/TextReader 我得到的错误是: Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.Harvester'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'configReader' to type TextReader. Make
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
):
但这会起作用(绑定configReader
:
[FunctionName("Harvester")]
public static async Task Run(
[TimerTrigger("0 */5 * * * *")]TimerInfo myTimer,
TraceWriter log,
TextReader 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路径的情况下使其工作的任何想法。
BlobAttribute
。我最好将blob配置保留在代码之外,这样我的函数将变得更加可移植。问题在于最新运行时支持新属性(configurationSource
)在function.json中
这告诉运行时使用config
(即function.json
)或C#属性进行函数配置
本质上允许您这样定义函数
[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": "..."
}
现在您可以将函数定义为
[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": "..."
}
使用这样一个更简单的配置
[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显示了如何执行此操作
您是否在使用VS 2017工具?如果是,您是否可以在输出文件夹中检查名为Harvester
的文件夹,该文件夹包含function.json
文件。您是否可以检查它是否有名为configurationSource
的属性以及它有什么值?我正在使用VS2017工具,检查了该文件,但没有此类属性ledconfigurationSource
。您是否碰巧提到了configReader
?如果是,这与我最初的问题中所述的一样(顺便说一句,上面的列表来自azure UI).no,configurationSource
是2017工具中引入的一个新属性,用于告诉函数运行时使用属性vs配置文件。您可以检查csproj中的``版本吗?最新版本是,尽管请注意,这应该将configurationSource
设置为属性,但您需要更改它到config
告诉运行时使用配置而不是属性。然后你需要在项目中包含该json文件。你的问题是关于Microsoft.NET.Sdk.Functions
包的,实际上我是在1.0.0-alpha3
上。切换到1.0.2
,然后configurationSrouce
重新启动谢谢!如果我在项目中包含JSON文件,它是否需要指向我的输出文件夹中的位置(bin\Debug…)?我已将其作为文件包含在我的源代码树中,但它仍会生成configurationSource:“attributes”
感谢您的分享,我可以确认它按预期工作。请将您的评论升级为答案,我将据此提供反馈。谢谢!