繁体   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