Welcome to Pete Brown's 10rem.net

First time here? If you are a developer or are interested in Microsoft tools and technology, please consider subscribing to the latest posts.

You may also be interested in my blog archives, the articles section, or some of my lab projects such as the C64 emulator written in Silverlight.

(hide this)

Silverlight 3 Quick Tip : Lambda Expressions for Async Web Service Calls

Pete Brown - 30 March 2009

(this should work in SL2 as well)

All network calls in Silverlight are asynchronous. The proliferation of event handling functions this can cause just makes for really cluttered code. Using lambda expressions seems to make my code a bit cleaner. (You can also use regular anonymous delegates, but the lambdas involve less typing)

public void LoadPolicyDetail()
{
    IvcDataServiceClient client = new IvcDataServiceClient();

    client.GetPolicyDetailCompleted += (s, e) =>
    {
        if (e.Result != null)
        {
            _policyDetail = e.Result;

            if (PolicyDetailLoaded != null)
                PolicyDetailLoaded(this, new EventArgs());
        }
        else
        {
            if (PolicyDetailLoadError != null)
                PolicyDetailLoadError(this, new EventArgs());
        }
    };

    client.GetPolicyDetailAsync();
}
The event handler is right in-line with the loading function. The event args remain strongly typed, so you have access to everything you normally would with an additional function.

In the example above, PolicyDetailLoaded and PolicyDetailLoadError are events I raise from my ViewModel, and GetPolicyDetailCompleted is the async call-completed event on the WCF client proxy.

   
posted by Pete Brown on Monday, March 30, 2009
filed under:    

14 comments for “Silverlight 3 Quick Tip : Lambda Expressions for Async Web Service Calls”

  1. Pete Brownsays:
    @Sergey I ran this across some other folks, and while this is by no means a definitive, verified answer, general concensus is that it will not leak.

    The lambda and wcf client are both eligable for collection after the GetPolicyDetailCompleted event is fired.

    If anyone wants to profile this and provide a definitive answer, I'll post the results.

    Pete
  2. samcovsays:
    Yes, this not only works fine in SL2, using Lambda functions works well for all other events as well.

    I love the way I don't have to declare the event type and sender, the Lambda just infers it... sweet!
  3. Mark Monstersays:
    Hi Pete,

    I've done similar things with lambda's. They really help making asynchronous code more readable.
    http://mark.mymonster.nl/2008/08/03/using-function-pointers-a-la-eventhandlert-instead-of-return-types/

    --
    Mark Monster
  4. Andrea Boschinsays:
    Hi Pete,

    I write a post most similar to your some months ago but it is slightly different. In my sample I've user Action<T> to forward success and fail of the asyncronous method to the caller. This is because I prefer to incapsulate service calls in a layer and let the caller doing his work with the results.

    http://blog.boschin.it/archive/2008/12/04/Silverlight-Gestire-la-comunicazione-asincrona.aspx
  5. Pete Brownsays:
    @Marc

    I typically did not reuse clients anyway, so it isn't a big deal. The overhead is pretty minimal.

    Creating new clients for each call let me both keep the code small and functional, and not lose track of which return came from which service call.

    Now, if you need to make a bunch of highly related calls, you may not want to go this route. I try to minimize service calls if possible (having larger calls rather than chatty calls), as those are the main performance sinks in a RIA

    Pete
  6. Petesays:
    @Rakhi

    Lambdas in VB don't work quite the same way, but you can achieve an almost identical result using an anonymous sub. See: http://10rem.net/blog/2010/04/16/tip-anonymous-event-handlers-in-vbnet

Comment on this Post

Remember me

2 trackbacks for “Silverlight 3 Quick Tip : Lambda Expressions for Async Web Service Calls”

  1. POKE 53280,0: Pete Brown's Blogsays:
    This is part 2 of a series. If you don’t yet have the Freescale board configured and tested with Windows