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
{
    [DataContract]
    public class Some
    {
        [DataMember]
        public string What;

        [DataMember]
        public string Why;
    }

    [ServiceContract]
    [JavascriptCallbackBehavior(UrlParameterName=”method”)]
    class MyService
    {
        [OperationContract]
        [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();
                endpoint.Behaviors.Add(behavior);
                endpoint.Behaviors.Add(scripting);
                serviceHost.Open();
                Console.WriteLine(“Press <ENTER> to terminate this program.”);
                Console.ReadLine();
            }
        }
    }
}

 

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” >
<head>
    <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);
        }
        $(document).ready(
            function () {
                $(“<p>Hello, I am Dynamic Text!</p>”).appendTo(“body”);
                $(“<p>Attempt</p>”).appendTo(“body”);
                $.getJSON(“http://localhost:8888/MyService/MyEndpoint/GetSome?method=?”, callback);
                $(“<p>Done</p>”).appendTo(“body”);
            }
        );
    </script>
</head>
<body>

</body>

</html>

Comments (0)