如何将function.json添加到现有的.NET函数2.0 [英] How to add function.json to existing .NET function 2.0

查看:61
本文介绍了如何将function.json添加到现有的.NET函数2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 func new --name MyHttpTrigger --template"HttpTrigger" 添加新函数时,在此函数中未创建 function.json ,当我尝试将其添加到当前目录并运行 func start --build ,我收到此错误:

when i add new function using func new --name MyHttpTrigger --template "HttpTrigger" there is no function.json created in this function, and when i tried to add it to current directory and run func start --build, i get this error :

您可以在这里找到我的 function.json 内容:

you can find here my function.json content :

{
  "disabled": false,
  "bindings": [
    {
      "authLevel": "anonymous",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "name": "res",
      "type": "http",
      "direction": "out"
    }
  ]
}

以前的httpTrigger函数

namespace final
{
    public static class httpTrigger
    {
        [FunctionName("httpTrigger")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            return name != null
                ? (ActionResult)new OkObjectResult($"Hello, {name}")
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
        }
    }
}

新的httpTrigger函数

namespace final
{
    public static class httpTrigger
    {
        public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            return name != null
                ? (ActionResult)new OkObjectResult($"Hello, {name}")
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
        }
    }
}

推荐答案

简短回答生成过程将根据为方法附加的属性,为.cs文件中定义的函数生成 function.json 文件.例如,Functions文档中有几个示例. BlobTrigger您不需要添加自己的 function.json .

Short Answer The build process takes care of generating function.json files for the functions you define in your .cs files based on the attributes you attach to your methods. There are several examples in the Functions docs, ex. a BlobTrigger function in C#. You shouldn't need to add your own function.json.

请参阅长答案"部分,以详细了解其在后台的工作方式.如果您看到针对已编译的C#函数描述的所有预期构建输出,并且函数运行时仍未找到任何函数,请检查是否在顶级目录上运行 func start --build 您的功能应用程序结构.

See the Long Answer section for a detailed breakdown of how this works behind the scenes. If you see all the expected build outputs described for a compiled C# function and the functions runtime still isn't locating any functions, check that you're running func start --build against the top-level directory of your function app structure.

详细回答您描述的行为是设计使然.听起来您已经习惯了脚本语言使用的Function的文件夹结构,例如.csx(C#脚本)文件.这是一个定义两个函数的示例, MyFunction MySecondFunction :

Long Answer The behavior you're describing is by design. It sounds like you're used to the folder structure of Functions used by scripting languages, such as for .csx (C# script) files. Here's an example that defines two functions, MyFunction and MySecondFunction:

FunctionApp
| - bin
| - MyFunction
| | - function.json
| | - run.csx
| - MySecondFunction
| | - function.json
| | - run.csx
| ...
| host.json
| local.settings.json

最初,Functions运行时仅识别此文件夹结构,并且C#函数只能用C#脚本编写.( MSDN杂志文章)后来,添加了常规的C#支持.我将把常规C#称为已编译的C#,以强调它与C#脚本之间的区别.

Originally, the Functions runtime only recognized this folder structure, and C# functions could only be written in C# script. (original blog announcement | MSDN magazine article) Later, regular C# support was added. I'll refer to regular C# as compiled C# to emphasize the distinction between it and C# script.

与已编译的C#函数应用程序相同的示例具有这样的文件夹结构:

The same example as a compiled C# function app has a folder structure like this:

FunctionApp
| - bin
| - obj
| - host.json
| - local.settings.json
| - FunctionApp.csproj
| - MyFunction.cs
| - MySecondFunction.cs

如果您构建此项目并展开 FunctionApp/bin 文件夹,则会看到以下内容:

If you build this project and expand the FunctionApp/bin folder, you'll see something like this:

FunctionApp
| - bin
| | - Debug
| | | - net461
| | | | - bin
| | | | - MyFunction
| | | | | - function.json
| | | | - MySecondFunction
| | | | | - function.json
| | | - host.json
| | | - local.settings.json
| | | - netstandard2.0
| | | | - …
| - obj
| - host.json
| - local.settings.json
| - FunctionApp.csproj
| - MyFunction.cs
| - MySecondFunction.cs

( netstandard2.0 文件夹将包含与 net461 文件夹类似的内容;它们只是不同的框架构建目标.)

(The netstandard2.0 folder will contain similar content as the net461 folder; they're just different framework build targets.)

请注意,已编译的C#函数应用程序的文件夹结构中的 FunctionApp/bin/Debug/net461与C#脚本应用程序的文件夹结构中的 FunctionApp 之间具有相似性.这是因为C#(不是C#脚本)功能应用程序的生成过程会使用.cs文件(例如,HttpTrigger`)中方法的属性来确定已定义了哪些功能,并以如下方式创建原始文件夹结构:其构建输出.

Note that the similarity between FunctionApp/bin/Debug/net461 in the compiled C# function app's folder structure andFunctionAppin the C# script app's folder structure. This is because the build process for C# (not C# script) function apps uses the attributes of the methods in the .cs files (ex.HttpTrigger`) to determine what functions have been defined and creates the original folder structure as its build output.

启动Azure Functions运行时(例如,通过 func host start )时,它不会查看 FunctionApp 来确定存在哪些函数并绑定绑定.它查看 FunctionApp/bin/Debug/net461/MyFunction .

When the Azure Functions runtime is started (ex. by func host start), it doesn't look at FunctionApp to figure out what functions exist and wire up bindings. It looks at FunctionApp/bin/Debug/net461/MyFunction.

在每个功能的文件夹中找到唯一的区别.在已编译的C#函数应用程序中,每个函数的文件夹在C#脚本函数应用程序中都没有.csx文件.仔细查看已编译的C#函数应用程序的 FunctionApp/bin/Debug/net461/MyFunction 文件夹中的 function.json ,您会看到类似以下的内容:

The only difference is found in the folders for each function. In the compiled C# function app, each function's folder lacks the .csx file in the C# script function app. Take a closer look at the function.json in the compiled C# function app's FunctionApp/bin/Debug/net461/MyFunction folder and you'll see something like this:

{
  "generatedBy": "Microsoft.NET.Sdk.Functions-1.0.13",
  "configurationSource": "attributes",
  "bindings": [
    {
      "type": "httpTrigger",
      "methods": [
        "post"
      ],
      "authLevel": "function",
      "name": "req"
    }
  ],
  "disabled": false,
  "scriptFile": "../bin/VSSample.dll",
  "entryPoint": "VSSample.HttpStart.Run"
}

与为C#脚本函数创建的 function.json 相比,已编译的C#函数的 function.json 具有一些额外的字段.这是每个函数对运行时的指示:

Compared to the function.json created for a C# script function, the compiled C# function's function.json has a few extra fields. Here's what each indicates to the functions runtime:

  • generateBy :此 function.json 是在编译过程中生成的,不是手动编写的
  • configurationSource :此 function.json 是从C#属性生成的
  • scriptFile :包含与此函数相对应的已编译代码的DLL的位置,相对于此 function.json 文件的位置
  • entryPoint :已编译DLL中函数的方法签名
  • generatedBy: this function.json was generated during compilation, not written by hand
  • configurationSource: this function.json was generated from C# attributes
  • scriptFile: the location of the DLL containing the compiled code corresponding to this function, relative to this function.json file's location
  • entryPoint: the method signature of the function within the compiled DLL

长话短说,已编译的C#函数应用程序在运行时依赖于与C#脚本函数应用程序相同的文件夹结构,但是它是由构建过程生成的,而不是由开发人员构建的.用已编译的C#编写功能应用程序的开发人员通过C#属性定义其绑定,而将 function.json 生成留给构建过程.

Long story short, a compiled C# function app relies on the same folder structure as a C# script function app at runtime, but it's generated by the build process instead of being constructed by the developer. A developer writing a function app in compiled C# defines their bindings through C# attributes, leaving function.json generation to the build process.

这篇关于如何将function.json添加到现有的.NET函数2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆