8

我想要一个 Http Trigger 函数来调用另一个 Http Trigger 函数。基本上,我试图通过 URL(HTTP 请求)访问触发器 1,触发器 1 将调用触发器 2。我想的是为触发器 2 放置修复 URL,所以你只需调用触发器 1。任何想法怎么做?

using System.Net;

public static async Task<HttpResponseMessage> Run(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);
}

任何帮助深表感谢。

4

3 回答 3

10

您可以使用它HttpClient来执行正常的 HTTP 请求。下面是调用函数的样子:

static HttpClient client = new HttpClient();
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req)
{
    var url = "https://<functionapp>.azurewebsites.net/api/Function2?code=<code>";
    var response = await client.GetAsync(url);
    string result = await response.Content.ReadAsStringAsync();
    return req.CreateResponse(HttpStatusCode.OK, "Function 1 " + result);
}
于 2017-11-23T23:11:45.370 回答
0

我认为您应该改用Durable Functions

public static async Task<object> Run(DurableOrchestrationContext ctx)
{
    var x = await ctx.CallActivityAsync<object>("YourOtherFunctionName");
    // Rest of Function code
}
于 2018-05-16T08:46:10.177 回答
0

这对我有用

var url ="azure function URL with code and params";
using var client = new HttpClient();
client.GetAsync(url).Wait();
于 2021-11-12T09:06:17.220 回答