从restful web服务返回位图

时间:2013-09-18 09:44:35

标签: c# wcf wcf-rest

Car.cs

[DataContract]
    public class Car
    {
         [DataMember]
         public string Id { get; set; }
         [DataMember]
         public Bitmap Image { get; set; }
    }

ICarService.cs

[ServiceContract]
    public interface ICarService
    {

        [OperationContract]
        [WebGet(UriTemplate = "Car/{id}")]

        Car GetCarId(string id);
    }

CarService.svc.cs

public class CarService : ICarService
    {

        public Car GetCarId(string id)
        {
            var newCar = new Car
            {
                Id = id,
                Image = new Bitmap("../../Atish.jpg")
            };
            return newCar;
        }
    }

的web.config

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="CarHaat.CarService">
        <endpoint address="" behaviorConfiguration="restfulBehavior"
          binding="webHttpBinding" bindingConfiguration="" contract="CarHaat.ICarService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/bookservice" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="restfulBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

问题是....当我在浏览器中点击此url localhost:4765 / CarService.svc / car / 1时,我得到以下异常。我怎么解决?我想以json格式返回一个图像。

enter image description here

2 个答案:

答案 0 :(得分:1)

什么是调用堆栈跟踪?如果我是你,我会去调试&gt;异常菜单,然后勾选Common Language Runtime Exceptions项的“thrown”复选框。然后,这将带您到达抛出异常的确切位置。对我来说,我相信它可能会从Bitmap构造函数中抛出,因为你给它一个不正确的路径。例如,您需要使用Server.MapPath将路径映射到相关图像文件。当涉及到Web平台时,它需要一个完全限定的文件路径。

除此之外,文件将被锁定,直到您处置位图对象,因此您可能会遇到问题。最好的办法是将图像文件的byte []返回给客户端,让客户端处理将其流式传输到页面(例如使用响应将字节写入页面)。如果你正在使用WCF平台并制作一个API系统的形式,这也是有意义的,你不需要使用.NET BCL,但要使它尽可能通用,大多数并非所有客户都能理解本机类型

答案 1 :(得分:0)

返回图像序列化为Base64(字符串),然后在客户端中反序列化

喜欢这个

//Serialize
var bytes = File.ReadAllBytes(@"bbbd996028159395cce9b63d717bf0ef.jpeg");
var base64 = Convert.ToBase64String(bytes);

//Deserialize
var nbytes = Convert.FromBase64String(base64);
File.WriteAllBytes(@"yayaya.jpeg", nbytes);