为什么Azure函数发送永不结束的HTTP请求?

时间:2018-10-19 03:20:22

标签: c# azure azure-functions webhooks serverless

下面我有一个azure函数,当我在聊天中提到我的漫游器时,我有webhook触发器。它触发了天蓝色的功能,但它会向我的房间发送无限的消息,因此我必须停止整个功能。

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

1 个答案:

答案 0 :(得分:1)

您需要将其绑定为HttpTrigger。当前,您的函数仅在运行时没有任何类型的触发。

类型很多。

  1. HttpTrigger
  2. TimerTrigger
  3. BlobTrigger
  4. QueueTrigger
  5. EventHubTrigger
  6. SendGridTrigger
  7. 其他很多,下面在Microsoft示例URL中提到

HttpTrigger的软件包:https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.Http

您可能还需要: https://www.nuget.org/packages/Microsoft.Azure.WebJobs

示例(来自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);
}