代码之家  ›  专栏  ›  技术社区  ›  elgrego

接收图像的WCF服务

  •  1
  • elgrego  · 技术社区  · 16 年前

    创建接受图像的Web服务的最佳方式是什么。 我已经写了一个接受二进制图像,但我觉得必须有一个更好的选择。

    2 回复  |  直到 15 年前
        1
  •  4
  •   core    16 年前

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

    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
    
    }
    
        2
  •  0
  •   Nathan    14 年前

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