A Windows Communication Foundation JSONP Service

All of the samples I’ve found that show how to build a JSONP service using the Windows Communication Foundation have the thing hosted in IIS.  IIS is literally the last place I want my Windows Communication Foundation services hosted … especially because I’ll only want them in IIS once I’ve debugged them, and debugging outside of IIS is much more efficient.  Here is a console version of such a service:


using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;

namespace Server
    public class Some
        public string What;

        public string Why;

    class MyService
        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        public Some GetSome(string input)
            return new Some() { What = “A Shotgun”, Why = “Needs an Outcome” };

    class Program
        const string BaseAddress = “http://localhost:8888/MyService/“;
        const string EndpointAddress = “MyEndpoint”;

        static void Main(string[] args)
            using(ServiceHostBase serviceHost = new ServiceHost(typeof(MyService),new Uri[]{new Uri(Program.BaseAddress)}))
                WebHttpBinding binding = new WebHttpBinding();
                binding.CrossDomainScriptAccessEnabled = true;
                ServiceEndpoint endpoint = serviceHost.AddServiceEndpoint(“Server.MyService”, binding, Program.EndpointAddress);
                WebHttpBehavior behavior = new WebHttpBehavior();
                WebScriptEnablingBehavior scripting = new WebScriptEnablingBehavior();
                Console.WriteLine(“Press <ENTER> to terminate this program.”);


And here is the HTML page that you can open in the current version of any major browser to see the output from the service:


<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml” >
    <title>Untitled Page</title>
    <script src=”http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.js” type=”text/javascript”></script>
    <script type=”text/javascript”>
        function callback(data) {
            alert(“Like a ” + data.What + ” ” + data.Why);
            function () {
                $(“<p>Hello, I am Dynamic Text!</p>”).appendTo(“body”);
                $.getJSON(“http://localhost:8888/MyService/MyEndpoint/GetSome?method=?”, callback);



Comments (0)

Skip to main content