Get an Image using WCF REST service

No.of Views1584
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  17 Apr 2010 23:04:35
Tag : WCF , REST Services
This article will give a very simple and basic explanation of , how to fetch an image using WCF REST service.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

This article will give a very simple and basic explanation of , how to fetch an image using WCF REST service.

Step 1

Create a WCF application.  To create a new application File -> New -> Web-> WCF Service Application.

 

Image Loading

Step 2

Remove all the default code created by WCF. Remove code from IService 1 interface and Service1 class.  Remove the code from Web.Config also. Open Web.Config and remove System.Servicemodel codes.

Step 3

Right click on Service1.svc  select View markup and add below code

 

Image Loading
<%@ ServiceHost Language="C#" Debug="true" Service="FetchingImageinBrowser.Service1" CodeBehind="Service1.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

Step 4

Create contract. Operation contract will return Stream. Stream is in the namespace System.IO. By putting WebGet attribute make operation contract

IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.IO ;
namespace FetchingImageinBrowser
{
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        [WebGet(UriTemplate = "GetImage", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
        Stream GetImage();
        
    }   
}

Step 5

Implement service. In service class write the below code.  Using FileStream  open and read the image. Then set outgoing response as image/jpeg using WebOperationContext class.

Service1.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.IO ; 

namespace FetchingImageinBrowser
{
    public class Service1 : IService1
    {
        public Stream GetImage()
        {
            FileStream fs = File.OpenRead(@"D:\a.jpg");
            WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
            return fs; 
        }
        
    }
}

Step 6

Press F5 to run the application. Append GetImage/ in URL to get the output.

Image Loading

 See the URL  
 

Image Loading

Conclusion

In this article, I discussed how to get an image from WCF REST service.  In next articles I will show complete Get and Set image using WCF Rest service.  Thanks for reading.
 

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
By:HuflitDate Of Posted:5/15/2012 4:51:06 AM
more
so i tried to create metadata in wep.config , but notthing change, so any one can help me.
By:HuflitDate Of Posted:5/15/2012 4:43:54 AM
Help me , I need your code now , plz
when run your code, I got a message failed like this : Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata. Error: Cannot obtain Metadata from http://localhost:4348/Service1.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:4348/Service1.svc Metadata contains a reference that cannot be resolved: 'http://localhost:4348/Service1.svc'. There was no endpoint listening at http://localhost:4348/Service1.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. The remote server returned an error: (404) Not Found.HTTP GET Error URI: http://localhost:4348/Service1.svc There was an error downloading 'http://localhost:4348/Service1.svc'. The request failed with HTTP status 404: Not Found.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top