Serving static web content using WCF

This post demonstrates how static web content can be served from a WCF service using WebHttpBinding and webHttp behavior.

Service Interface

The service has just one method in its interface called StaticContent.

using System.IO;
using System.ServiceModel;

namespace WwwService
    interface IWwwService
        Stream StaticContent(string content);

Service Implementation

The implementation of the service is shown below. The StaticContent method returns a FileStream with the content of the requested resource, and sets the content type and HTTP status code.

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

namespace WwwService
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class WwwServiceImplementation : IWwwService
        [WebInvoke(Method = "GET",
            BodyStyle = WebMessageBodyStyle.Bare,
            UriTemplate = "/{*content}")]
        public Stream StaticContent(string content)
            OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse;
            string path = "www/" + (string.IsNullOrEmpty(content) ? "index.html" : content);
            string extension = Path.GetExtension(path);
            string contentType = string.Empty;

            switch (extension)
                case ".htm":
                case ".html":
                    contentType = "text/html";
                case ".jpg":
                    contentType = "image/jpeg";
                case ".png":
                    contentType = "image/png";
                case ".ico":
                    contentType = "image/x-icon";
                case ".js":
                    contentType = "application/javascript";
                case ".json":
                    contentType = "application/json";

            if (File.Exists(path) && !string.IsNullOrEmpty(contentType))
                response.ContentType = contentType;
                response.StatusCode = System.Net.HttpStatusCode.OK;
                return File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                response.StatusCode = System.Net.HttpStatusCode.NotFound;
                return null;

You may want to add additional content types to the switch statement above, or implement an externally configurable mapping scheme.

Hosting the service

Here’s the App.config for the service.

<?xml version="1.0" encoding="utf-8"?>
        <binding name="webHttpBinding"/>
        <behavior name="webHttp">
      <service name="WwwService.WwwServiceImplementation">
        <endpoint address="http://localhost:8003/wwwservice" binding="webHttpBinding" bindingConfiguration="webHttpBinding" contract="WwwService.IWwwService" behaviorConfiguration="webHttp"/>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>

Here’s a console program that hosts the service configured above.

using System;
using System.ServiceModel;

namespace WwwService
    class Program
        static void Main(string[] args)
            ServiceHost host = new ServiceHost(typeof(WwwService.WwwServiceImplementation));

            Console.WriteLine("Hit Enter to quit.");

Where to put static content

Create a folder called www inside the folder where the service is started, store your static content there, and name the default web page index.html. Then, when the browser requests content starting with the URL http://localhost:8003/wwwservice, it will be served static content by the StaticContent method, or returned a Not Found status code.


Leave a Reply

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

You are commenting using your 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