Web API - 在异步操作仍处于挂起状态时完成的异步模块或处理程序

时间:2014-09-30 14:04:32

标签: c# asp.net-mvc asynchronous asp.net-web-api async-await

无法想出这个。想法?

[控制器]

await _imageRepo.Resize(name, width, height, queryParams, (image) =>
{
    response = createResponse(image);
});

[库]

public async Task Resize(string name, string width, string height, ImageOptions options, Action<Images> callback)
{
    var actionName = "resized";
    var newWidth = Convert.ToInt32(width);
    var newHeight = string.IsNullOrEmpty(height) ? newWidth : Convert.ToInt32(height);
    var resizedName = ApplyOptionName(string.Format("{0}-{3}-{1}x{2}", name, newWidth, newHeight, actionName), options);

    await Get(resizedName, null, async (previousImage) =>
    {
        if (previousImage != null)
        {
            callback(previousImage);
            return;
        }

        await Get(name, null, image =>
        {
            if (image == null)
            {
                callback(null);
                return;
            }

            using (ImageFactory imageFactory = new ImageFactory())
            {
                imageFactory.Load(image.ToStream());

                imageFactory.Resize(new ResizeLayer(new Size(newWidth, newHeight), ResizeMode.Max, AnchorPosition.Left));

                ProcessImageOptions(imageFactory, options);

                using (MemoryStream ms = new MemoryStream())
                {
                    imageFactory.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

                    var newImage = Create(new Images(ms) { Name = resizedName });

                    callback(newImage);
                }
            }

        });

    });

}

[异常]

[InvalidOperationException: An asynchronous module or handler completed while an asynchronous operation was still pending.]

所有这些回调都不适合我的胃,但为什么我会收到这个错误?

1 个答案:

答案 0 :(得分:0)

虽然回调不是很好。我们决定继续使用这种模式。这意味着要弄清楚如何制作这些lambda的返回任务。

这很容易。

正如斯蒂芬·克利里所写的“将它们改为Func&lt; ...,任务&gt; ......”。

所以这是最后的实施。

public async Task Resize(string name, string width, string height, ImageOptions options, Func<Images, Task> callback)
{
    var actionName = "resized";
    var newWidth = Convert.ToInt32(width);
    var newHeight = string.IsNullOrEmpty(height) ? newWidth : Convert.ToInt32(height);
    var resizedName = ApplyOptionName(string.Format("{0}-{3}-{1}x{2}", name, newWidth, newHeight, actionName), options);

    await Get(resizedName, null, async (previousImage) =>
    {
        Images resizedImage = null;

        if (previousImage != null)
        {
            resizedImage = previousImage;
        }
        else
        {
            await Get(name, null, async (image) =>
            {
                if (image == null)
                {
                    resizedImage = null;
                    return;
                }

                using (ImageFactory imageFactory = new ImageFactory())
                {
                    imageFactory.Load(image.ToStream());

                    imageFactory.Resize(new ResizeLayer(new Size(newWidth, newHeight), ResizeMode.Max, AnchorPosition.Left));

                    ProcessImageOptions(imageFactory, options);

                    using (MemoryStream ms = new MemoryStream())
                    {
                        imageFactory.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

                        resizedImage = await CreateAsync(new Images(ms) { Name = resizedName });
                    }
                }

            });
        }

        await callback(resizedImage);

    });

}

一个小的重复因素......但没有造成伤害。

感谢您的帮助!