The world’s simplest WCF Web API

We all have to start somewhere, and I like to start with the simplest possible thing that works.  So I created a Console application and used NuGet to pull in the WebAPI.All package which contains all my dependencies.[1]

Once that was done, all I need is this:

using System;
using System.Net.Http;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Microsoft.ApplicationServer.Http;

namespace SampleApi {
    class Program {
        static void Main(string[] args) {
            var host = new HttpServiceHost(typeof (ApiService), "http://localhost:9000");
            host.Open();
            Console.WriteLine("Browse to http://localhost:9000");
            Console.Read();
        }
    }

    [ServiceContract]
    public class ApiService {

        [WebGet(UriTemplate = "")]
        public HttpResponseMessage GetHome() {
            return new HttpResponseMessage() {
                Content = new StringContent("Welcome Home", Encoding.UTF8, "text/plain")
            };

        }
    }

}

 

If you point a browser to http://localhost:9000 you will get your first result.  Stay tuned for more exciting things to come.

 

[1]  Make sure you have the latest version of Nuget installed (ie. one from within the last week or so).  Also,  I swore at the project for a while until I realized that it was currently using the Client Profile version of the Framework.  This project needs the full version. Arrgh!

comments powered by Disqus