Monday 15 December 2008

Hallo World Asynchronous WCF calls from WPF

Here is how to call the default service in a non blocking way in WPF:


Add the reference
using System.ComponentModel;

Then the code looks like:

namespace AsynchronousNonBlocking
{
public partial class Window1 : Window
{

ServiceReference1.Service1Client test2;

public Window1()
{
InitializeComponent();
test2 = new ServiceReference1.Service1Client();
}

private void btnSynchronous_Click(object sender, RoutedEventArgs e)
{
ServiceReference1.Service1Client test = new ServiceReference1.Service1Client();
test.DoWork();
}

private void btnAsynchronous_Click(object sender, RoutedEventArgs e)
{
test2.DoWorkCompleted += new EventHandler(DoWorkCallback);
test2.DoWorkAsync();
}

public event System.EventHandler DoWorkCompleted;

static void DoWorkCallback(object sender, AsyncCompletedEventArgs e)
{
Console.WriteLine("Done");
}
}
}