用于接收图像的WCF服务

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

请先 登录 后评论
本文连接: http://www.china-sunrider.com.cn/question/10846
source: https://stackoverflow.com/questions/104797

2 个回答

core

这张图片“存在于哪里?”是否可以在本地文件系统或网络上访问?如果是这样,我建议您的 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

}
请先 登录 后评论
Nathan

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

至少,我就是这么做的。

请先 登录 后评论
user contributions licensed under CC BY-SA.