HttpWebRequest奇怪的行为

时间:2013-02-18 05:25:43

标签: c# asp.net-mvc httpwebrequest

更新 看起来这个问题与this有关。我有一大堆代码要处理:

dynamic facebookResponse = facebookClient.Batch(new FacebookBatchParameter(HttpMethod.Get, "me"), new FacebookBatchParameter(HttpMethod.Get, "me", new { fields = "picture.type(large)" }));

Errr不确定为什么会发生这种情况!

我发现了一些奇怪的行为HttpWebRequest。

// Send the image to blob storage.
var url = facebookResponse[1]["picture"]["data"]["url"];
byte[] imageBytes;
var request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Timeout = 5000;
request.ReadWriteTimeout = 20000;
var response = (HttpWebResponse)request.GetResponse();

var validationResults = _imageService.UploadProfilePicture(response, url, newUserId).ToList();

在最后一行,它抛出了一个错误'object' does not contain a definition for 'ToList',但如果我在最后一行之前取出所有行,它将进入该函数。

可能是线程问题?

编辑: _imageService只是通过Ninject传递给控制器​​的依赖项。

这是UploadProfilePicture函数:

public IEnumerable<ValidationResult> UploadProfilePicture(Stream s, string fileName, int userId)
{
    if (s == null) throw new ArgumentNullException("s");
    if (userId <= 0) throw new ArgumentNullException("userId");

    if (s.Length <= 0)
    {
        yield return new ValidationResult("", GlobalResources.FileContainsNoContent);
        yield break;
    }

    var maxProfilePictureSize = int.Parse(ConfigurationManager.AppSettings[ConfigurationManagerConstants.BlobMaxProfilePictureSize]);
    if (s.Length > maxProfilePictureSize)
    {
        yield return new ValidationResult("", string.Format(GlobalResources.ProfilePictureMustBeNoGreaterThan,
            ConfigurationManager.AppSettings[ConfigurationManagerConstants.BlobMaxProfilePictureSizeFriendlyText]));
        yield break;
    }

    const string defaultExtensionType = ".jpg";
    var container = GetCloudBlobContainer(ConfigurationManager.AppSettings[ConfigurationManagerConstants.BlobImageContainerName]);
    var uniqueBlobName = string.Format(ConfigurationManager.AppSettings[ConfigurationManagerConstants.BlobUserProfilePicturePath], userId, defaultExtensionType);
    var blob = container.GetBlockBlobReference(uniqueBlobName);
    blob.Properties.ContentType = "image/jpeg";

    var uploadImageExtension = Path.GetExtension(fileName);
    if (!string.IsNullOrWhiteSpace(uploadImageExtension) && !uploadImageExtension.Equals(defaultExtensionType, StringComparison.InvariantCultureIgnoreCase))
    {
        using (var ms = new MemoryStream())
        {
            ImageBuilder.Current.Build(s, ms, new ResizeSettings("format=jpg"));
            ms.Seek(0, SeekOrigin.Begin);
            blob.UploadFromStream(ms);
        }
    }
    else
    {
        blob.UploadFromStream(s);
    }
}

0 个答案:

没有答案