Create Image Server using WCF WEB HTTP Service

Last post I wrote on WCF Web HTTP Service: why and how. This post was for beginners in which I discussed how to get it started and write your fist WCF WEB HTTP Service. In this post we will go one step further and learn to work with streams or to be precisely images. Service will return image based on the input from client. We are going to create an image server. Image server will do following task

  • Read requested image from file system on server
  • Convert that to stream
  • Return stream to clients

We are going to create WCF WEB HTTP Service which will act as image server. So to start with let us create Service Contract.

 


using System.IO;
using System.ServiceModel;
using System.ServiceModel.Web;

namespace Demo1
{    
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet]
        Stream FetchImage(string imageName);      
       
    }
}


In above Service, there is one Operation Contract which returns a stream. Operation Contract can be accessed using baseurl/FetchImage?imageName={imagename with extension}. Client will pass image name in query parameter to fetch a particular image. If image is not present on image then null would be returned from service.

Service implementation is very simple. You need to do following steps to fetch image and return it as stream.

Step 1: First check for existence of the image. If it exists got to step 2 else go to step 4

Step 2: Read image as FileStream

Step 3: Set Outgoing Request Content Type as image/jpeg

Step 4: convert string Image does not exist as stream and return

Service is implemented as below,


public class Service1 : IService1
    {       

        public Stream FetchImage(string imageName)
        {
            string filePath = @"c:\\" + imageName;

            if (File.Exists(filePath))
            {
                FileStream fs = File.OpenRead(filePath);
                WebOperationContext.Current.OutgoingRequest.ContentType = "image/jpeg";
                return fs;
            }
            else
            {
                byte[] byteArray = Encoding.UTF8.GetBytes(" Requested Image does not exist :(");
                MemoryStream strm = new MemoryStream(byteArray);
                return strm;
            }
        }        
    }


Service implementation is very simple but one line of code in which we are explicitly saying that outgoing response content type is image/jpeg

image

Already we have covered this in previous post about EndPoint configuration but for quick reference open markup of .svc file and add use Factory method System.ServiceModel.Activation.WebServiceHostFactory to create WEB HTTP Service.

image

This is what all you need to do to create an Image Server as WCF WEB HTTP Service. You can test server from IE browser.

image

And if requested image is not present then error message will be rendered as below,

image

Now you can go ahead and consume and work with this image server in any kind of client able to perform HTTP GET operation. In further post we will get into details of consuming this service from various kind of clients. I hope this post will help you.

One thought on “Create Image Server using WCF WEB HTTP Service

  1. Pingback: Dew Drop – March 11, 2014 (#1740) | Morning Dew

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 )

Google+ photo

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

Connecting to %s