简体   繁体   中英

Why Azure function sending never ending HTTP Requests?

I have an azure function below, I have webhook trigger when i mention my bot in my chat. It triggers the azure Function but it sends infinite messages back to my room and I have to stop the entire function.

     #r "Newtonsoft.Json"

    using System.Net; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Primitives; using Newtonsoft.Json;

    public static async Task<object> Run(HttpRequestMessage req, TraceWriter log) {
        string jsonContent = await req.Content.ReadAsStringAsync();
        var dto = JsonConvert.DeserializeObject<RootObject>(jsonContent);

        string callerName = dto.data.personEmail;

            using (var httpClient = new HttpClient())
            {
            using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://****************"))
                {
                    request.Headers.TryAddWithoutValidation("Cache-Control", "no-cache");
                    request.Headers.TryAddWithoutValidation("Authorization", "Bearer *****************************************************");
                    request.Headers.TryAddWithoutValidation("Postman-Token", "**********************"); 

                    var multipartContent = new MultipartFormDataContent();
                    multipartContent.Add(new StringContent(callerName), "markdown");
                    multipartContent.Add(new StringContent("***********************************"), "roomId");
                    request.Content = multipartContent; 

                    var response = await httpClient.SendAsync(request);
                }
            }    

        return req.CreateResponse(HttpStatusCode.OK); 
}
 public class Data {
        public string id { get; set; }
        public string roomId { get; set; }
        public string roomType { get; set; }
        public string personId { get; set; }
        public string personEmail { get; set; }
        public List<string> mentionedPeople { get; set; }
        public DateTime created { get; set; } 
}

    public class RootObject {
        public Data data { get; set; } 
}

You need to bind it as an HttpTrigger. Currently, your function is just running without any type of triggering.

There are many types.

  1. HttpTrigger
  2. TimerTrigger
  3. BlobTrigger
  4. QueueTrigger
  5. EventHubTrigger
  6. SendGridTrigger
  7. Many others, mentioned below in Microsoft example URL

Package for HttpTrigger: https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.Http

You may also need: https://www.nuget.org/packages/Microsoft.Azure.WebJobs

Example(from https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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