HttpContent.CopyToAsync会暂时挂起

时间:2018-01-29 12:04:25

标签: c# xml azure asynchronous httpclient

我们有一个Azure WebJob,它每15秒触发一次Timer来轮询外部Xml数据源。该请求返回带有流量数据的2.4 MB XML文件。在读取数据之前,该方法会根据上次接收的数据中的值检查LastModified标头值 - 这与数据馈送供应商设置的建议一致。

WebJob工作数天/周没有问题,但偶尔会在将XML数据读入内存流进行处理时挂起。 WebJob日志中的最后一个条目是“将内容复制到内存流”。

    HttpRequestMessage webRequest = new HttpRequestMessage(HttpMethod.Get, feedPath);

using (var _webApiHttpClient = new HttpClient())
{
    _webApiHttpClient.BaseAddress = new Uri(SettingsManager.Instance.BaseUrl);

    _webApiHttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));

    log.WriteLine(webRequest.RequestUri.ToString());
    using (HttpResponseMessage response = await _webApiHttpClient.SendAsync(webRequest, HttpCompletionOption.ResponseHeadersRead))
    {
        var dateReceived = DateTime.Now.ToUniversalTime();
        log.WriteLine("Request result: {0}", response.StatusCode);

        if (response.IsSuccessStatusCode)
        {
            log.WriteLine("Received feed data");
            DateTimeOffset lastRead = new DateTimeOffset();
            if (lastChangedBlob.Exists())
            {
                using (var stream = lastChangedBlob.OpenRead())
                {
                    BinaryFormatter f = new BinaryFormatter();
                    lastRead = (DateTimeOffset)f.Deserialize(stream);
                }
            }

            log.WriteLine("Content last modified {0} and last read {1}", response.Content.Headers.LastModified, lastRead);
            var modified = response.Content.Headers.LastModified;

            if (modified.HasValue && modified.Value > lastRead)
            {

                log.WriteLine("Starting copy content to memory stream");

                using (Stream stream = new MemoryStream())
                {
                    await response.Content.CopyToAsync(stream);
                    log.WriteLine("Copy completed: response.Content.CopyToAsync");
                    // Go back to start of stream to save file to BLOB
                    stream.Seek((long)0, SeekOrigin.Begin);
                    log.WriteLine("Seeked begining of stream ready to ProcessFeed");
                    await ProcessFeed(type, output, queue, log, dateReceived, stream);
                    log.WriteLine("ProcessFeed completed");
                }

...

0 个答案:

没有答案