Before this week I never really bothered much with WCF. My reasons were two fold, one it always seemed like a pain to configure and the second reason was the fact that it was overkill for the projects that I was working on. That being said, I finally hit a project where it made sense to use it. I needed things like the ability to easily do JSON and XML requests, access through more than just a web request, etc. Anyway, I set myself up with a new project space, followed a couple tutorials, figured out how to host the services in a web app separate from my other apps so that the services would be centralized, etc. Basically everything was going fine... Well until I tried to make my requests from web app 1 (the project that needed access to the service) to web app 2 (the app actually holding the service). It kept coming back with a failure message.
How could this be? I set up my service contract correctly, my function even had the necessary WebGet attribute:
That one attribute attached to my function should set it up for JSON. I had my service reference set up in my script manager. I even had the WCF Service file set up correctly using the WebScriptServiceHostFactory:
1: <%@ ServiceHost Service="Services.MyService" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>
According to the documentation I wouldn't even need to bother with configuring the config file. I could strip out the system.serviceModel portion and that factory should handle things for me. Hell if I called it in the browser, it passed back the JSON correctly so what the hell was going on? JSONP... That's right, in all of my haste I had completely forgotten about JSONP (JSON with Padding). JSONP, in a very basic description, is a usage pattern that allows a secondary server (not the primary server) to use JSON in a meaningful way. In otherwords think cross site scripting, which is exactly what I was trying to do. My calls from Sys.Net.WebServiceProxy.invoke (long story as to why I'm not able to simply use jQuery) were requesting JSONP to be sent back and were failing when it didn't receive it. So I went searching online as to how to enable it in WCF.
Apparently I'm the only person out there who has decided to work with .Net 4.0 on anything and especially WCF. Well either that or no one wanted to talk about it. Anyway, after about 2 hours of digging, I finally found some semblance of help on this subject. You see prior to 4.0, the way you were suppose to set up JSONP was that you had to define a number of classes including a message encoder, a message encoder factory, an operationbehaviorattribute, etc. It was a pain and every post about it said just wait until 4 comes out as it will have it by default... Well it does:
1: <?xml version="1.0"?>
5: <compilation debug="false" targetFramework="4.0" />
8: <modules runAllManagedModulesForAllRequests="true"/>
13: <behavior name="webHttpBehavior">
14: <webHttp />
20: <binding name="webHttpBindingJsonP" crossDomainScriptAccessEnabled="true"></binding>
24: <service name="Services.MyService">
25: <clear />
26: <endpoint address="" binding="webHttpBinding" bindingConfiguration="webHttpBindingJsonP"
27: contract="Services.Interfaces.IMyService" behaviorConfiguration="webHttpBehavior" />
30: <add baseAddress="http://SERVICELOCATION" />
The bit above is the web.config file for the service web app. It turns out the whole Factory attribute in the svc file is kind of lie, you do indeed need the system.serviceModel section. Anyway, in it we define a endpoint behavior for webHttpBehavior and a binding that uses webHttpBinding called webHttpBindingWithJsonP. The special thing about the binding is the crossDomainScriptAccessEnabled attribute is set to true. This one item enables it. Past that we just set up our service and an endpoint using the binding that we've created and that's it (and of course set up the base address). Two hours and that's all I needed to do. Anyway, I hope it helps someone out there so you don't have to deal with the headache that I've had today. So give it a try, leave feedback, and happy coding.