簡體   English   中英

在等待110秒后再次從我的操作中觸發,從另一個Web API調用Web API

[英]Calling a web api from another web api, after 110 seconds waiting my action fires again

我有一個Web Api 2,它正在調用另一個Web Api。 場景很簡單,我從Angular.Js上傳文件,然后Angular調用了一個Web api,這個Web api調用了另一個Web api。 最后一個Web API需要2分鍾才能響應,因此,當第一個Web API超時(在110秒后)時,它將再次被觸發。 我在干凈的環境中重現了此問題,它是Web Api附帶的。 問題是我不知道如何告訴我的Web API,我需要更多時間來接收請求的答案。

[HttpPost]
[MultipartContentValidator]
[ActionName("uploadfile")]      
// POST /api/documents/uploadfile?folderId={folderId}&assetId={assetId}
public async Task<HttpResponseMessage> UploadFile(int folderId, int assetId)
{
    IExternalWebApiCaller _caller = new ExternalWebApiCaller();
    //## If it is not multipart, we throw it away
    if (!Request.Content.IsMimeMultipartContent("form-data"))
    {
        throw new HttpResponseException(HttpStatusCode.BadRequest);
    }

    //## Getting the juice in memory instead of the harddrive
    var provider = await Request.Content.ReadAsMultipartAsync<InMemoryMultipartFormDataStreamProvider>(new InMemoryMultipartFormDataStreamProvider());

    //## We get access to the data
    NameValueCollection formData = provider.FormData;

    //## It will access to the files
    IList<HttpContent> files = provider.Files;

    //## Reading a file as bytearray and creating the model
    //## with the filename and stuff...if any
    if (files != null)
    {
        HttpContent filetobeuploaded = files[0];
        byte[] filebytearray = await filetobeuploaded.ReadAsByteArrayAsync();

        DocumentUploadViewModel model = new DocumentUploadViewModel();
        model.AssetId = assetId;
        model.FolderId = folderId;
        model.Data = filebytearray;
        model.Filename = filetobeuploaded.Headers.ContentDisposition.FileName.Replace("\"", "");

        return await _caller.CallWebApiHttpResponseMessage("api/document/uploadfile", HttpMethod.Post, null, model, GetHeaders());
    }
    else
    {
        //## Something fail (file with no content), so we kick this out
        throw new HttpResponseException(HttpStatusCode.NoContent);
    }
}

任何想法如何避免這種情況?

Wep Api沒有內置的機制來使長請求超時,這意味着無法延長當前超時時間。

最好的方法是將文件拆分為多個包,並一一發送小包,直到通過兩個API完成文件為止。 最后一個API將程序包存儲在Azure Blob存儲中。

感謝您的答復。

暫無
暫無

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

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