简体   繁体   English

调试Http触发器Azure函数VS 2017

[英]Debugging Http Trigger Azure Function VS 2017

I have Visual Studio 2017 15.3 and have installed the Azure Development workload installed. 我拥有Visual Studio 2017 15.3,并且已安装Azure开发工作负载。

I created a blank HttpTrigger according to this article: 我根据这篇文章创建了一个空白的HttpTrigger

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

I can debug successfully if I use Name as parameter query string. 如果使用Name作为参数查询字符串,则可以成功调试。

However, If I use Postman to create this Post request: 但是,如果我使用邮递员创建此Post请求:

{
    "name": "Azure"
}

I get the following error: 我收到以下错误:

"mscorlib: Exception while executing function: HttpTrigger . Anonymously Hosted DynamicMethods Assembly: 'Newtonsoft.Json.Linq.JObject' doe s not contain a definition for 'name'." “ mscorlib:执行函数:HttpTrigger时发生异常。匿名托管的DynamicMethods程序集:'Newtonsoft.Json.Linq.JObject'doe不包含'name'的定义。”

Here is my code for the function app in Visual Studio 2017: 这是我在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);
        }
    }
}

Also, if I copy the exact same code within the Run function and use the same test post in a function app via the Azure Portal it all works fine. 另外,如果我在“运行”函数中复制完全相同的代码,并通过Azure门户在函数应用程序中使用相同的测试文章,则一切正常。

在此处输入图片说明

I create a Azure function app with your code and send a request using Postman, which works fine on my side. 我用您的代码创建了一个Azure函数应用程序,并使用Postman发送了一个请求,这对我来说很好。

Request and Response 请求和回应

在此处输入图片说明

Request body: 要求正文:

在此处输入图片说明

The breakpoint at getting request body can be hit as expected 可以按预期命中获取请求正文的断点

在此处输入图片说明

edit: 编辑:

I can extract name from data without any error. 我可以从data提取name ,而不会出现任何错误。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM