Question

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>

Problem is .... when i hit this url localhost:4765/CarService.svc/car/1 in browser, I get following exception. How can i resolve it?? I want to return an image in json format.

enter image description here

Was it helpful?

Solution

what is the call stack trace? if I were you, I would go to the debug > Exception menu and then tick the "thrown" check box for the Common Language Runtime Exceptions item. This will then take you to exactly where the exception is being thrown from. To me, I believe it could be thrown from the Bitmap constructor as you are giving it an incorrect path. you need to map the path to the image file in question by using Server.MapPath for example. It needs a fully qualified path to the file in question when it comes to the web platform.

in addition to that, the file will be locked until you dispose of the bitmap object so you may have issues. The best thing to do is probably return the byte[] of the image file to the client and let the client side deal with streaming it to the page (for example using the response to write the bytes to the page). it also makes sense if you are using the WCF platform and making a form of an API system, where you are not tied to using the .NET BCL's but to make it as generic as possible that majority of not all of the clients understand the native types

OTHER TIPS

Return the image Serialized in Base64 (string) and then in the client Deserialize it

Like this

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

//Deserialize
var nbytes = Convert.FromBase64String(base64);
File.WriteAllBytes(@"yayaya.jpeg", nbytes);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top