创建用于接受图像的Web服务的最佳方法是什么。 图像可能非常大,我不想更改Web应用程序的默认接收大小。 我写了一个接受二进制图像但我认为必须有更好的选择。

有帮助吗?

解决方案

此图片在哪里“生活?”是可以在本地文件系统中还是在Web上访问?如果是这样,我建议您让WebService接受URI(可以是URL或本地文件)并将其作为Stream打开,然后使用StreamReader读取它的内容。

示例(但在FaultExceptions中包装异常,并添加FaultContractAttributes):

using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Sockets;

[OperationContract]
public void FetchImage(Uri url)
{
    // Validate url

    if (url == null)
    {
        throw new ArgumentNullException(url);
    }

    // If the service doesn't know how to resolve relative URI paths

    /*if (!uri.IsAbsoluteUri)
    {
        throw new ArgumentException("Must be absolute.", url);
    }*/

    // Download and load the image

    Image image = new Func<Bitmap>(() =>
    {
        try
        {
            using (WebClient downloader = new WebClient())
            {
                return new Bitmap(downloader.OpenRead(url));
            }
        }
        catch (ArgumentException exception)
        {
            throw new ResourceNotImageException(url, exception);
        }
        catch (WebException exception)
        {
            throw new ImageDownloadFailedException(url, exception);
        }

        // IOException and SocketException are not wrapped by WebException :(            

        catch (IOException exception)
        {
            throw new ImageDownloadFailedException(url, exception);
        }
        catch (SocketException exception)
        {
            throw new ImageDownloadFailedException(url, exception);
        }
    })();

    // Do something with image

}

其他提示

您无法使用FTP将图像上传到服务器,那么当完成后服务器(以及WCF服务)可以轻松访问它吗?这样你就不需要思考者使用接收大小设置等。

至少,我就是这样做的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top