如何添加function.json到现有的.NET 2.0的功能

问题描述 投票:0回答:1

当我使用添加新func new --name MyHttpTrigger --template "HttpTrigger"功能没有在这个函数创建function.json,当我试图将其添加到当前目录和运行func start --build,我得到这个错误:

No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).

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

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

Pervious httpTrigger function

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

New httpTrigger function

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

c# azure azure-functions serverless
1个回答
2
投票

简答构建过程负责生成function.json您在根据您连接到你的方法的属性你的.cs文件中定义的功能文件。还有在功能文档,前几个例子。 a BlobTrigger function in C#.你不应该需要添加自己的function.json

看到这是如何工作在幕后的详细分解长的答案部分。如果你看到所有已编译的C#函数和函数运行时仍然没有任何定位功能描述预期的生成输出,检查你正在运行func start --build对你的应用程序的功能结构的顶级目录。

龙回答您所描述的行为是设计使然。这听起来像你已经习惯了通过脚本语言,如.csx(C#脚本)文件中使用的功能的文件夹结构。下面是一个定义了两个函数,MyFunctionMySecondFunction一个例子:

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

原来,该功能仅运行时认识到这个文件夹结构,和C#的功能只能在C#脚本编写。 (original blog announcement | MSDN magazine article)后,加入常规的C#的支持。我将把常规C#编译好的C#来强调它和C#脚本之间的区别。

相同的例子作为已编译的C#功能的应用程序有一个文件夹结构是这样的:

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

如果你建立这个项目,并扩大FunctionApp/bin文件夹,你会看到这样的事情:

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文件夹,它们只是不同的框架构建目标。)

需要注意的是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`之间的相似性),以确定哪些职能已经确定并创建原来的文件夹结构,其生成的输出。

当Azure的功能运行系统启动(例如,通过func host start),它不看FunctionApp找出存在的功能及导线上绑定。它着眼于FunctionApp/bin/Debug/net461/MyFunction

唯一不同的是在每个功能的文件夹中找到。在编译的C#应用​​程序的功能,每个功能的文件夹缺少在C#脚本功能的应用程序的.csx文件。仔细看看在编译的C#应用​​程序功能的function.json文件夹中的FunctionApp/bin/Debug/net461/MyFunction,你会看到这样的内容:

{
  "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具有一些新的领域。以下是各种指示的功能运行:

  • generatedBy:编译期间生成此function.json,而不是手写
  • configurationSource:此function.json从C#属性生成
  • scriptFile:含有对应于该功能的编译的代码的DLL的位置,相对于该function.json文件的位置
  • entryPoint:函数的编译的DLL中的方法签名

长话短说,一个编译的C#功能的应用程序依赖于相同的文件夹结构在运行一个C#脚本功能的应用程序,但它通过构建过程,而不是由开发商正在建造中产生。开发人员在编写C#写一个函数的应用程序通过C#属性定义它们的绑定,让function.json代构建过程。

© www.soinside.com 2019 - 2024. All rights reserved.