Azure Functions 错误 - 无法将参数绑定到类型字符串
Azure Functions error - Cannot bind parameter to type String
我正在尝试使用 Azure 函数将文件保存到 FTP。 json 是这样的:
{
"type": "apiHubFile",
"name": "outputFile",
"path": "{folder}/ps-{DateTime}.txt",
"connection": "ftp_FTP",
"direction": "out"
}
函数代码是这样的:
public static void Run(string myEventHubMessage, TraceWriter log, string folder, out string outputFile)
{
var model = JsonConvert.DeserializeObject<PalmSenseMeasurementInput>(myEventHubMessage);
folder = model.FtpFolderName;
outputFile = $"{model.Date.ToString("dd.MM.yyyy hh:mm:ss")};{model.Concentration};{model.Temperature};{model.ErrorMessage}";
log.Info($"C# Event Hub trigger Save-to-ftp function saved to FTP: {myEventHubMessage}");
}
我得到的错误是这样的:
Function ($SaveToFtp) Error: Microsoft.Azure.WebJobs.Host: Error
indexing method 'Functions.SaveToFtp'. Microsoft.Azure.WebJobs.Host:
Cannot bind parameter 'folder' to type String. 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.).
如果我将 {folder} 替换为文件夹名称,它会起作用:
"path": "psm/ps-{DateTime}.txt"
为什么?难道不能从代码中改路径吗?
folder
是你的函数的输入参数,它不会影响输出绑定。
{folder}
语法的意思是运行时将尝试在您的输入项中找到 folder
属性,并绑定到它。
所以试试下面的方法:
public static void Run(PalmSenseMeasurementInput model, out string outputFile)
{
outputFile = $"{model.Date.ToString("dd.MM.yyyy hh:mm:ss")};{model.Concentration};{model.Temperature};{model.ErrorMessage}";
}
与 function.json
:
{
"type": "apiHubFile",
"name": "outputFile",
"path": "{FtpFolderName}/ps-{DateTime}.txt",
"connection": "ftp_FTP",
"direction": "out"
}
您可以在 "Binding expressions and patterns" 和 "Bind to custom input properties in a binding expression" 部分阅读更多内容 here。
我正在尝试使用 Azure 函数将文件保存到 FTP。 json 是这样的:
{
"type": "apiHubFile",
"name": "outputFile",
"path": "{folder}/ps-{DateTime}.txt",
"connection": "ftp_FTP",
"direction": "out"
}
函数代码是这样的:
public static void Run(string myEventHubMessage, TraceWriter log, string folder, out string outputFile)
{
var model = JsonConvert.DeserializeObject<PalmSenseMeasurementInput>(myEventHubMessage);
folder = model.FtpFolderName;
outputFile = $"{model.Date.ToString("dd.MM.yyyy hh:mm:ss")};{model.Concentration};{model.Temperature};{model.ErrorMessage}";
log.Info($"C# Event Hub trigger Save-to-ftp function saved to FTP: {myEventHubMessage}");
}
我得到的错误是这样的:
Function ($SaveToFtp) Error: Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.SaveToFtp'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'folder' to type String. 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.).
如果我将 {folder} 替换为文件夹名称,它会起作用:
"path": "psm/ps-{DateTime}.txt"
为什么?难道不能从代码中改路径吗?
folder
是你的函数的输入参数,它不会影响输出绑定。
{folder}
语法的意思是运行时将尝试在您的输入项中找到 folder
属性,并绑定到它。
所以试试下面的方法:
public static void Run(PalmSenseMeasurementInput model, out string outputFile)
{
outputFile = $"{model.Date.ToString("dd.MM.yyyy hh:mm:ss")};{model.Concentration};{model.Temperature};{model.ErrorMessage}";
}
与 function.json
:
{
"type": "apiHubFile",
"name": "outputFile",
"path": "{FtpFolderName}/ps-{DateTime}.txt",
"connection": "ftp_FTP",
"direction": "out"
}
您可以在 "Binding expressions and patterns" 和 "Bind to custom input properties in a binding expression" 部分阅读更多内容 here。