Why don't you create the proxy on each call? Then you don't have to bother to 
unsubscribe, because the proxy object goes away... Also, you could inline that 
subscription into a lambda, like so:

proxy.FooCompleted += (s, e) => callback(bar);

Alternatively you could use something like nServiceBus, which helps you not to 
manage all kinds of (failure) scenarios manually.

Cheers,
Adi

From: [email protected] [mailto:[email protected]] On 
Behalf Of Greg Keogh
Sent: Tuesday, April 13, 2010 11:57 AM
To: 'ozWPF'
Subject: Service calls to Action<>

Folks, what is the neatest coding way of converting an async WCF service call 
into an Action<> callback? Here is a sample of "student" code that does this 
crudely:

public void AddTest(int num1, int num2, Action<int> callback)
{
       proxy.AddTestCompleted += new 
EventHandler<AddTestCompletedEventArgs>(Proxy_AddTestCompleted);
       proxy.AddTestAsync(num1, num2, callback);
}

private void Proxy_AddTestCompleted(object sender, AddTestCompletedEventArgs e)
{
       Action<int> callback = (Action<int>)e.UserState;
       callback(e.Result);
}

This works, but this is in an app-lifetime class and the event hander keeps 
getting added on each call, so I would now have to balance the += add handler 
with a -= remove handler in the Completed method. This is ugly and not thread 
safe.

How do others deal with this pattern in a nice neat way? I know that some MVVM 
pattern frameworks do this and hide the complexity, but I'm not using such a 
framework at the moment and I'm curious about what they do internally. I've 
seen Jordan and Paul run demos with Action callbacks from service calls, but I 
didn't see the underlying code. Can you guys point me to some samples?

Greg
_______________________________________________
ozwpf mailing list
[email protected]
http://prdlxvm0001.codify.net/mailman/listinfo/ozwpf

Reply via email to