Get an Image using WCF REST service

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.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 2

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


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

Step 3

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

IService1.cs

1 using System;

2 using System.Collections.Generic;

3 using System.Linq;

4 using System.Runtime.Serialization;

5 using System.ServiceModel;

6 using System.ServiceModel.Web;

7 using System.Text;

8 using System.IO;

9 namespace FetchingImageinBrowser

10 {

11 [ServiceContract]

12 public interface IService1

13 {

14

15 [OperationContract]

16 [WebGet(UriTemplate = “GetImage”, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]

17 Stream GetImage();

18

19 }

20 }

21

22

23

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

1 using System;

2 using System.Collections.Generic;

3 using System.Linq;

4 using System.Runtime.Serialization;

5 using System.ServiceModel;

6 using System.ServiceModel.Web;

7 using System.Text;

8 using System.IO;

9

10 namespace FetchingImageinBrowser

11 {

12 public class Service1 : IService1

13 {

14 public Stream GetImage()

15 {

16 FileStream fs = File.OpenRead(@”D:\a.jpg”);

17 WebOperationContext.Current.OutgoingResponse.ContentType = “image/jpeg”;

18 return fs;

19 }

20

21 }

22 }

23

Step 4

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

8 responses to “Get an Image using WCF REST service”

  1. good article

  2. […] on returning image from WCF REST Service Full article about this video can be read here  #mce_temp_url# By Dhananjay Kumar, on June 9, 2010 at 9:49 am, under Video, WCF, WCF 4.0, WCF Video. . No […]

  3. Good one… keep it up Kumar

  4. hey buddy,
    thanks for your sharing such a nice article but am unable to execute it successfuly. am getting this error

    Error: Cannot obtain Metadata from http://localhost:51190/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:51190/Service1.svc Metadata contains a reference that cannot be resolved: ‘http://localhost:51190/Service1.svc’. There was no endpoint listening at http://localhost:51190/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:51190/Service1.svc There was an error downloading ‘http://localhost:51190/Service1.svc’. The request failed with HTTP status 404: Not Found.

  5. is the 404 error fixed? this happens for large file uploads – any help?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com