Azure Functions error - Cannot bind parameter to type String

余生颓废 提交于 2021-02-08 20:05:00

问题


I am trying to save files to FTP by using an Azure Function. The json is this:

{
      "type": "apiHubFile",
      "name": "outputFile",
      "path": "{folder}/ps-{DateTime}.txt",
      "connection": "ftp_FTP",
      "direction": "out"
}

The function code is this:

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}");

}

The error I get is this:

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

If I replace {folder} with a folder name it works:

"path": "psm/ps-{DateTime}.txt"

Why? Is it not possible to change the path from the code?


回答1:


folder is an input parameter of your function, it can't affect the ouput binding.

What {folder} syntax means is that the runtime will try to find folder property in your input item, and bind to it.

So try the following instead:

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}";
}

with function.json:

{
      "type": "apiHubFile",
      "name": "outputFile",
      "path": "{FtpFolderName}/ps-{DateTime}.txt",
      "connection": "ftp_FTP",
      "direction": "out"
}

You can read more here, in "Binding expressions and patterns" and "Bind to custom input properties in a binding expression" sections.



来源:https://stackoverflow.com/questions/45371396/azure-functions-error-cannot-bind-parameter-to-type-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!