簡體   English   中英

如何使 API 調用等待之前的調用完成

[英]How to make an API call wait until the previous calls have been completed

我在 c# (MYAPI) 中有一個 API,它接受請求,並向 Unity (UNITYAPI) 發出 API 調用以執行某些操作。

流程應該是這樣的

客戶端 -> MYAPI -> UnityAPI -> 執行任務 -> 從 UnityAPI 到 MYAPI 的響應 -> 對客戶端的響應。

現在,客戶端可以通過不同的線程調用MYAPI。 MYAPI 應該等待前面的 MYAPI 調用完成(這反過來意味着 UNITYAPI 完成了一些操作)。 MYAPI "CANNOT" 向 UNITYAPI 發送任何請求,直到之前的 MYAPI 調用響應回客戶端。

MYAPI 中的 Function 使 api 調用 UNITY API:

static async Task<string> PostURI(HttpContent c)
        {
            Uri uri = new Uri("http://localhost:4444");
            var response = string.Empty;
            using (var client = new HttpClient())
            {
                HttpResponseMessage result = null;

                try
                {
                    result = await client.PostAsync(uri, c);

                }
                catch (Exception ex)
                {
                    throw ex;
                }

                if (result.IsSuccessStatusCode)
                {
                    response = result.StatusCode.ToString();
                }
            }
            return response;
        }

MYAPI 中的 Function 處理所有調用:

public void ProcessCalls(string operation)
        {

            HttpContent next = new StringContent(operation);

            while (isBlocked == true)
            {

            }

            isBlocked = true;

            var t = Task.Run(() => PostURI(next));
            t.Wait();

            isBlocked = false;

        }

在這里,您可以看到我所做的解決方法。 我剛剛使用了一個 static 變量 isBlocked 使任何調用在無限循環中等待,直到完成任何其他操作。

當我向 MYAPI 發送 2 個並行調用時,這工作正常。 第二個調用等待第一個調用完成,然后繼續向 UNITYAPI 發送調用並等待。 但是當我並行發送 3 個調用時,它會中斷。 第一個呼叫統一完成,但沒有收到任何呼叫的響應。 必須有一種優雅的方式來做到這一點。

所以我想要的是:-客戶端應該能夠向 MYAPI 發送多個請求。 - 每個請求都應該等到所有先前的請求都將響應發送回客戶端。

提前致謝。

編輯 1:調用 ProcessCalls 的方法:


        [HttpPost]
        [Route("MyPostRoute")]
        public async void  MyPostRoute()
        {
            var request = new StreamReader(Request.Body).ReadToEnd();

            // Doing some validations on the request
            if (request.isValid())
            {
                await helperobject.ProcessCalls(request);
                //helperobject is an object of the class which has the two functions mentioned above.
            }
            else
                throw new Exception("Input was not in Correct format");
        }

使用信號量,您需要將其設為 static,因此該 class 的所有實例都使用相同的。 然后你可以像這樣 go :

private static SemaphoreSlim processCallsSemaphore = new SemaphoreSlim(1,1);
// new SemaphoreSlim(1,1); => start with 1 count available and allow at max 1.

public async Task ProcessCalls(string operation)
{

    HttpContent next = new StringContent(operation);

    try
    {
        await processCallsSemaphore.WaitAsync();
        await PostURI(next);  // Assuming this is also an async API or can be made one.
    }
    finally
    {
        processCallsSemaphore.Release();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM