【问题标题】:Debugging Http Trigger Azure Function VS 2017调试 Http Trigger Azure Function VS 2017
【发布时间】:2017-10-10 09:40:01
【问题描述】:

我有 Visual Studio 2017 15.3 并安装了 Azure 开发工作负载。

我根据这篇文章创建了一个空白HttpTrigger

http://www.c-sharpcorner.com/article/getting-started-with-microsoft-azure-functions-using-visual-studio-2017/

如果我使用Name作为参数查询字符串,我可以调试成功。

但是,如果我使用 Postman 创建此 Post 请求:

{
    "name": "Azure"
}

我收到以下错误:

"mscorlib:执行函数时出现异常:HttpTrigger .匿名托管 DynamicMethods 程序集:'Newtonsoft.Json.Linq.JObject' doe s 不包含 'name' 的定义。”

这是我在 Visual Studio 2017 中的函数应用代码:

using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;

namespace VS2017TestFunctionApp
{
    public static class HttpTrigger
    {
        [FunctionName("HttpTrigger")]
        public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");

            // parse query parameter
            string name = req.GetQueryNameValuePairs()
                .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
                .Value;

            // Get request body
            dynamic data = await req.Content.ReadAsAsync<object>();

            // Set name to query string or body data
            name = name ?? data?.name;

            return name == null
                ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
                : req.CreateResponse(HttpStatusCode.OK, "Hello " + name);
        }
    }
}

另外,如果我在 Run 函数中复制完全相同的代码,并通过 Azure 门户在函数应用程序中使用相同的测试帖子,则一切正常。

【问题讨论】:

    标签: c# function azure


    【解决方案1】:

    我使用您的代码创建了一个 Azure 函数应用,并使用 Postman 发送请求,这对我来说工作正常。

    请求和响应

    请求正文:

    获取请求正文的断点可以按预期命中

    编辑:

    我可以从data 中提取name 而不会出现任何错误。

    【讨论】:

    • 如果你从你的断点移动到下一行到'name',你应该得到错误。
    • 另外,如果我在 Run 函数中复制完全相同的代码并通过 Azure 门户在函数应用程序中使用相同的测试帖子,则一切正常。
    • If you move to the next line from your break point to 'name' you should get the error. 我没有收到错误,代码工作正常。
    • 嗯,有趣,我使用的是 .Net 4.6.1。这对我来说似乎是一个版本控制问题..
    猜你喜欢
    • 2019-11-06
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 2019-04-29
    • 2020-11-07
    • 2021-11-05
    • 2022-06-16
    • 2021-11-09
    相关资源
    最近更新 更多