IMPORTANT NOTEThis article is depreciated as of about SF8. For services please instead use ServiceStack which is included in sitefinity. About a billion times faster and way less code. Here's a quick Gist that contains all the code you need.
I've always been an asmx guy mostly for the reason that they're crazy simple to implement. I mean you just drop a couple files in your webroot, code a quick function with whatever return type and it just always seems to work.
So sitefinity comes along build using mostly WCF and I have no idea what I'm doing :) Most blog posts on WCF make it seem crazy complicated with lots code and web.config updates. Furthermore the documentation over on sitefinity.com (while thorough) WAY over-complicates how simple it is to generate a service. There's like 7-10 pages of overviews of the archetecture, code, samples, examples, wowza...for a WCF newb it's intimidating. Here's a quick example: You're new to WCF, this is your sample...but much to take in at a glance. HOWEVER, these are STILL good articles once you know what you're doing and comfortable using WCF! For example this is a good thing to know, and you can see it in action inspecting the JSON allover Sitefinity.
So lets just take it back to basics, and do a "Hello World" sample in as little code as possible. Then you can just take the concepts, revisit the docs, and go wild (nerd it up).
Step 1: Design your Service, we're using REST now, not old-school "methods", get in the game nancy!
This is how I think of this step, it's like adding a layer to asmx saying when a request comes in that looks "like this", send the results over to "here".
So you can see that I have three Hello samples, and the basic gist of it is that the curly braces in the Uri match the parameters in the method below...simple! (and badass). So when a request comes in that matches the signature in "UriTemplate", it sends the data to the corresponding method.
Step 2: Implement the new methods
So in a separate file you want to then create a class that implements that interface and use either VS2010 or JustCode to help implement the methods. You can do that by just hovering over the interface name and using the popups the tools give you. JustCode goes a step farther by underlining the interface name red showing you that you have methods that need implementing, and it won't go away until you do :)
Step 3: Create the .svc file jQuery will...query
Ok, now I know this point might be a hot topic since Sitefinity implemented virtual paths, however I still much prefer to have a physical .svc file I can locate on the filesystem. I will rescind that statement when\if Sitefinity can start aggregating services so I can browse them somehow to see what I have available.
So the code below goes in a .svc file you make and that's what you point your jQuery ajax calls at.
- Sample1: http://www.site.com/HelloWorldService.svc/hello
"Hi there buddy"
- Sample2: http://www.site.com/HelloWorldService.svc/hello/steve
"Nice to see you again steve"
- Sample3: http://www.site.com/HelloWorldService.svc/hello/steve?lastname=scott&age=30
"Nice to see you again steve scott, 30 seems old"
- Note: http://www.site.com/HelloWorldService.svc/help will show you all the Uris you're exposing automatically
So clearly there's a lot more, and that's all covered in the Sitefinity Documentation. Like returning objects, PUT\DELETE requests, etc. The purpose of this blog post however was just to get you started with a simple service to get off the asmx habit :)