简体   繁体   中英

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

I have a Web Api 2 it is calling another Web Api. The scenario is simple, I am uploading a file from Angular.Js then Angular calls a web api, this web api calls another web api. The last web api takes 2 minutes to respond, so when the first web api times out (after 110 seconds), it is fired again. I have reproduced this issue in a clean environment and it is something that comes with Web Api. The problem it is I don't know how to tell my web api, I need more time to receive the answer from my request.

[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);
    }
}

Any ideas how to avoid that?

Wep Api doesn't have a built-in mechanism to time out long requests, that means there is not way to extend the current time out.

The best approach come by splitting the file in packages and send small packages one by one until the file is complete over both API's. The last API will store the packages in Azure Blob Storage.

Thanks for the replies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM