Okay, WCF, we can be friends now

Over the years, I've had a tough time with Windows Communication Foundation, otherwise know as (and sometimes cursed as) WCF. I knew it was what I was "supposed" to be using to because it handled complex scenarios like managing access as secured messages passed through systems and users with different access rights. However, it didn't seem to be able to handle my simple scenarios – things like returning very simple, unrestricted information from a server to a client – without requiring hours of pain, configuration, and things like writing a custom ServiceHostFactory.

I understand the idea of WCF – it’s an entire API that’s designed to allow for just about anything you might want to do in a SOA scenario, and it offers a pretty big abstraction over the transport mechanism so you can swap out SOAP over HTTP to Binary over TCP with a few well-placed config settings, and you can theoretically do it without caring about the transport or wire format.

So, I guess my thoughts on WCF up until very recently can be summed up as follows:

  • I knew it did a lot of neat, advanced things that I never seemed to need to do
  • WCF configuration always seemed to be a problem for me in rather simple use, e.g. hosting a basic, unauthenticated WCF service in an MVC application on shared hosting
  • My co-workers got sick of trying to explain to me why I shouldn’t just use an ASMX service

All that’s changing, though. There have been two new developments in WCF-land that have broken down the walls I’d built around my heart:

Favorite new features in WCF Web API’s

Focus

First, the overall focus on HTTP as a good thing, not something to be abstracted away. There can be value in a variety of approaches, and those values can shift over time. WCF’s original design seemed to be oriented towards letting coders write code without worrying about the transport protocol. That can be useful in cases, but the fact is that HTTP contains some great features that we’ve been ignoring. This seems a little similar to the difference in approaches between ASP.NET Web Forms / ASP.NET MVC. In some cases an abstraction can help you be more productive by handling insignificant details for you, but in other cases those details are significant and important.

And I think the variety in focus is a good thing. Different software challenges call for different tools. For instance, looking at the WCF offerings:

  • WCF Data Services is a great way to expose data in a raw format which is queryable and accessible in a great, standards friendly format (OData)
  • WCF RIA Services solves some common client-server communication issues, such as keeping business logic in sync on both client and server through some pretty slick cross-compilation
  • [more WCF options here – IANAWE (I am not a WCF Expert)]
  • Now, WCF Web API’s provide an option for writing pure, RESTful services and service clients that work close to the HTTP level with minimal friction

Pipeline

Rather than stack piles of attributes on your service methods or write a ton of configuration, you can surgically process the input and output using lightweight processors. For instance, you can create a processor that displays information for a specific media type, like JSON or an image format like PNG. What’s really powerful here is that these processors can be associated with a media type, so they’re automatically returned to clients of your service based on what media types they prefer, as stated in the Accept header.

More on that in the next post, where I talk about using the Speech API to create a SpeechProcessor that responds to clients which accept audio/x-wav.

Queryable services

Glenn’s announcement post shows the promise here – you can simply  annotate a service with the [QueryComposition] which will allow clients to query over a service’s results. Glenn’s post shows how this works pretty well, so I’m just quoting it here. First, a service is marked as queryable:

[WebGet(UriTemplate = "")]
[QueryComposition]
public IEnumerable<Contact> Get()
{
    return contacts.AsQueryable();
}

Now we can query over the results on the client. For instance, if we wanted to get all contacts in Florida, we could use a request like "http://localhost:8081/contacts?$filter=State%20eq%20FL" which says "find me the contact with a State equal to FL".

Client-side Features

There are some great client-side features which mirror new features on the server-side. For instance, once your services are exposed as queryable, there’s client-side support for query via LINQ. There’s support via HttpRequestMessage and HttpResponseMessage to the HTTP values on the client as well as on the server.

The new HttpClient provides a client which was built with the same philosophy and API support on the client to match what’s on the server. I’ll plan to look at HttpClient first when I need to query services from .NET code.

WCF jQuery Support

There’s some interesting work in progress to build out support for writing services which are accessible from jQuery. While I’d probably use ASP.NET MVC’s JSON support for simple cases, if I needed to build out a full API which exposed services over AJAX, this looks really useful.

JsonValue

The jQuery support gets even more interesting with new support for JsonValue. Tomek spells it out in a great post covering WCF support for jQuery, but I think broad support for JsonValue in the .NET Framework deserves some individual attention because it enables some really slick things like LINQ to JSON:

JsonObject body;
string[] favoriteToys =
  (from child in (JsonValue)body.AsDynamic().Children
   where child.Value.AsDynamic().Name.ReadAs<string>(string.Empty).StartsWith("J")
   select child.Value.AsDynamic().BestToy.ReadAs<string>("No favorite toy")).ToArray();

Where to go next

There’s a lot to the new WCF Web API’s, and I’m only scratching the surface in this overview. Check out my next post to see a sample processor, and take a look at the list of posts listed in the News section at http://wcf.codeplex.com/ for more info.

Some top resources:

11 Comments

  • Everything clear and in good shape. Thanks for sharing such information in your post. Could you specify more code examples on CodePlex.com ?

  • Thanks for sharing so aamzing article with us…I was looking for something like this…I found it quiet interesting, hopefully you will keep posting such blogs….Keep sharing

  • I leave a response each time I like a article on a site or I have something to
    add to the conversation. It is caused by
    the passion communicated in the post I browsed.
    And on this article Okay, WCF, we can be friends now - Jon Galloway.
    I was excited enough to drop a thought ;-) I do have 2 questions for you if it's allright. Could it be just me or do a few of the remarks appear like left by brain dead people? :-P And, if you are posting at additional places, I'd like to keep up with anything new you have to post.
    Could you list all of all your community pages like your twitter feed, Facebook page
    or linkedin profile?

  • These are actually impressive ideas in regarding blogging.
    You have touched some fastidious points here. Any way keep up wrinting.

  • choose the mum to be photo appointment for example the practiced solution
    sweets buffet feature innovations

  • Fastidious response in return of this issue with solid
    arguments and explaining everything concerning that.

  • Ephedrine hcl is generally considered to be the best was rightly speaking about most of the effective supplements that increase the number of products that they are unable to move even a
    step. Not green coffee bean extract side effects only will it play
    havoc with your weight? Seniors may be buying more green tea now that a compound found
    in herbal green coffee bean extract side effects
    are right for your situation. Workouts are the best product that will actually work.

  • Hmm it seems like your site ate my first comment (it was super long) so I
    guess I'll just sum it up what I had written and say, I'm thoroughly enjoying your blog.
    I too am an aspiring blog blogger but I'm still new to the whole thing. Do you have any recommendations for first-time blog writers? I'd really appreciate it.

  • I've been exploring for a little for any high quality articles or weblog posts in this kind of area . Exploring in Yahoo I ultimately stumbled upon this website. Studying this info So i'm satisfied
    to convey that I have an incredibly excellent uncanny feeling I came
    upon exactly what I needed. I such a lot indisputably will make sure
    to do not omit this website and provides it a look on a relentless
    basis.

  • Do you mind if I quote a few of your articles as long as I provide credit
    and sources back to your blog? My blog is in the very same niche as yours and my visitors would genuinely benefit
    from some of the information you provide here. Please let me know if this
    alright with you. Thanks!

  • What i do not realize is actually how you're no longer actually much more well-preferred than you might be now. You are very intelligent. You already know thus significantly relating to this subject, made me in my view imagine it from a lot of numerous angles. Its like men and women aren't interested unless it's one thing to do with Woman gaga! Your personal stuffs nice. All the time care for it up!

Comments have been disabled for this content.