My previous article was about WCF Web HTTP Service: why and how. That article was for beginners in which I explained how to get started and write your fist WCF WEB HTTP Service. In this article we will go one step further and learn to work with streams, or to be precise, images.
The Service will return an image based on the input from the client. We will create an image server. The image server will do the following:
- Read requested image from file system on server
- Convert that to stream
- Return stream to clients
We will create a WCF WEB HTTP Service that will act as an image server. So to begin let us create a 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 the above Service, there is an Operation Contract that returns a stream. The Operation Contract can be accessed using baseurl/FetchImage?imageName={imagename with extension}. The client will pass an image name in the query parameter to fetch a specific image. If the image is not present on the image then null would be returned from the service.
The Service implementation is very simple. You need to use the following procedure to fetch the image and return it as a stream.
Step 1: First check for the existence of the image. If it exists then got to step 2 else go to step 4
Step 2: Read the image as a FileStream
Step 3: Set the Outgoing Request Content Type to "image/jpeg"
Step 4: convert the string image that does not exist as a stream and return
The 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;
}
}
}
The Service implementation is very simple but there is one line of code in which we are explicitly saying that an outgoing response content type is image/jpeg.
Already we have covered this in the previous article (
WCF Web HTTP Service: why and how) about EndPoint configuration but for a quick reference open the markup of the .svc file and add a use of the Factory method System.ServiceModel.Activation.WebServiceHostFactory to create the WEB HTTP Service.
This is what you all need to do; create an Image Server as a WCF WEB HTTP Service. You can test the server from the IE browser.
And if the requested image is not present then an error message will be rendered as below:
Now you can proceed to and consume and work with this image server in any kind of client able to perform a HTTP GET operation. In a future article we will get into the details of consuming this service from various kinds of clients. I hope this article will help you.