最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to customize the response from the HttpTrigger Azure function apps in .NET 8 isolated worker process - Stack Overflow

programmeradmin5浏览0评论

We have a HttpTrigger Azure function app and this is calling a durable function. This durable function is returning a HttpResponseData after calling the CreateCheckStatusResponseAsync.

We weren't able to create a custom response and getting the response from CreateCheckStatusResponseAsync only.

Please advice how to customise this response.

This is the sample code:

// Function app
[Function("HelloCities_HttpStart")]
public static async Task<HttpResponseData> HttpStart([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
        [DurableClient] DurableTaskClient client,
        FunctionContext executionContext)
{
    ILogger logger = executionContext.GetLogger("HelloCities_HttpStart");

    // Function input comes from the request content.
    string instanceId = await client.ScheduleNewOrchestrationInstanceAsync(nameof(HelloCities));

    logger.LogInformation("Started orchestration with ID = '{instanceId}'.", instanceId);

    // Returns an HTTP 202 response with an instance management payload.
    // See 
    return await client.CreateCheckStatusResponseAsync(req, instanceId);
}

The response that we are expecting should be like this:

But the response we are getting instead is:

We have a HttpTrigger Azure function app and this is calling a durable function. This durable function is returning a HttpResponseData after calling the CreateCheckStatusResponseAsync.

We weren't able to create a custom response and getting the response from CreateCheckStatusResponseAsync only.

Please advice how to customise this response.

This is the sample code:

// Function app
[Function("HelloCities_HttpStart")]
public static async Task<HttpResponseData> HttpStart([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,
        [DurableClient] DurableTaskClient client,
        FunctionContext executionContext)
{
    ILogger logger = executionContext.GetLogger("HelloCities_HttpStart");

    // Function input comes from the request content.
    string instanceId = await client.ScheduleNewOrchestrationInstanceAsync(nameof(HelloCities));

    logger.LogInformation("Started orchestration with ID = '{instanceId}'.", instanceId);

    // Returns an HTTP 202 response with an instance management payload.
    // See https://learn.microsoft/azure/azure-functions/durable/durable-functions-http-api#start-orchestration
    return await client.CreateCheckStatusResponseAsync(req, instanceId);
}

The response that we are expecting should be like this:

But the response we are getting instead is:

Share Improve this question edited 3 hours ago marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked 6 hours ago Anuraj RajagopalAnuraj Rajagopal 355 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

When you call await client.ScheduleNewOrchestrationInstanceAsync it's only scheduling the Orchestration to start. That will take a finite amount of time.

Meanwhile, you immediately proceed to checking the Orchestration status, even though it probably hasn't had time to start yet.

There are a few solutions.

  1. Not very elegant: add a Task.Delay of a few seconds after scheduling the Orchestration, to allow time for it to start. This is pretty bad, because there's no guarantee how long you need to wait so it will sometimes fail; and it will slow your HTTP request.

  2. Better: make your Function return a 202 Accepted response^1 with another URL (another Function) that they can call to check the status of the Orchestration, and move that code into there.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论