Welcome to irritatedVowel.com Sign in | Help

POKE 53280,0: Pete Brown's Blog

Silverlight, WPF, Windows Client Development, Woodworking, .NET Programming, CNC, Nature, and other topics.

Subscribe

Subscribe to my feed
Add to Technorati Favorites

My Book

Order my upcoming book, Silverlight in Action, covering Silverlight 4, ViewModel/MVVM, WCF RIA Services, MEF and more

About Pete Brown

Pete Brown is a Microsoft Developer Division Community Program Manager, focusing on Windows Client Development as well as a former Microsoft Silverlight MVP and INETA Speaker. Pete writes on a number of topics including Silverlight, WPF, .NET, woodworking and working as a consultant in the DC area. read more

Community Events


who's online

AddThis Social Bookmark Button

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

(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.

  Add to Technorati Favorites
Posted: Monday, March 30, 2009 1:40 AM by Pete.Brown
Filed under: ,

Comments

Sergey Barskiy said:

Would this introduce a memory leak or two event handlers if LoadPolicyDetail is called twice? Thanks. Sergey Barskiy.
# March 30, 2009 9:12 AM

JWC said:

I like it. Thanks.
# March 30, 2009 12:23 PM

Pete.Brown said:

@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

# March 30, 2009 1:11 PM

samcov said:

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!
# March 30, 2009 6:39 PM

CodeThinked said:

Heisenberg And Lambdas
# March 30, 2009 11:07 PM

Mark Monster said:

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
# April 1, 2009 2:37 AM

Andrea said:

Great! Does it works also on SL2?
# April 1, 2009 6:31 AM

Pete.Brown said:

@Andrea

Yes

# April 1, 2009 8:05 AM

Mark said:

If only VB had multi-line lambdas.... NOW :)
# April 3, 2009 3:18 PM

Andrea Boschin said:

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 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
# April 7, 2009 7:31 PM

Marc Perrone said:

This looks nice but do you really want the overhead of creating a new client and lambda object every time you need to make a service call?
# April 14, 2009 1:07 PM

Pete.Brown said:

@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

# April 14, 2009 1:33 PM

POKE 53280,0: Pete Brown's Blog said:

This is part 2 of a series. If you don’t yet have the Freescale board configured and tested with Windows
# November 1, 2009 6:29 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Enter the text you see in the image:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS