In this sequel we’ll consume the WCF service that we hosted in IIS in our previous sequel.  We’ll modify the WPF client that we have already built and have consumed the registration service via SOAP, to consume via REST as well.  For reference, the application user interface appeared as in the diagram below:

We had not coded the click events for the buttons on the right hand side of the interface with trailing (rest) labels.  We’ll do that now.  So, take the following steps:

  1. Directly reference the registration service library from within the client application or create a new IRegistration class replica in the client application.  This will be used as proxy
  2. Reference the System.ServiceModel.Web library to use the WebChannelFactory
  3. Wire up the previously mentioned click events similar to the sample below

private void btnListRest_Click(object sender, RoutedEventArgs e)
{
    WebChannelFactory<Demo.Wcf.Lib.IRegistration> factory = new WebChannelFactory<Demo.Wcf.Lib.IRegistration>(new Uri("http://localhost/Demo.Wcf.Host.Rest/RegistrationService.svc/"));
    Demo.Wcf.Lib.IRegistration svc = factory.CreateChannel();
    dg.ItemsSource = svc.GetRegistrationList();
}

private void btnRegisterRest_Click(object sender, RoutedEventArgs e)
{
    WebChannelFactory<Demo.Wcf.Lib.IRegistration> factory = new WebChannelFactory<Demo.Wcf.Lib.IRegistration>(new Uri("http://localhost/Demo.Wcf.Host.Rest/RegistrationService.svc/"));
    Demo.Wcf.Lib.IRegistration svc = factory.CreateChannel();
    Demo.Wcf.Lib.Registration reg = new Demo.Wcf.Lib.Registration();
    reg.RegistrantName = txtRegistrant.Text;
    svc.Register(reg);
    dg.ItemsSource = svc.GetRegistrationList();
}

private void btnDeleteRest_Click(object sender, RoutedEventArgs e)
{
    WebChannelFactory<Demo.Wcf.Lib.IRegistration> factory = new WebChannelFactory<Demo.Wcf.Lib.IRegistration>(new Uri("http://localhost/Demo.Wcf.Host.Rest/RegistrationService.svc/"));
    Demo.Wcf.Lib.IRegistration svc = factory.CreateChannel();
    object selectedValue = dg.SelectedValue;
    if (selectedValue != null)
    {
        string id = (dg.SelectedValue as Demo.Wcf.Lib.Registration).Id;
        if (!string.IsNullOrEmpty(id))
        {
            svc.DeleteRegistration(id);
            dg.ItemsSource = svc.GetRegistrationList();
        }
    }
}

Note that for WCF service consumption via REST, no service references are required.  We should now have the RESTful part of our WPF client for our WCF registration service fully operational.

In the next sequel we’ll create a Silverlight client to consume this WCF service.