RSS

RESTfully consume WCF service

29 Sep

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.

 

About nazmulababy

Service to take over the world
3 Comments

Posted by on September 29, 2009 in .Net WCF

 

3 Responses to RESTfully consume WCF service

  1. Indu

    March 24, 2011 at 2:56 pm

    Sir,
    I have been practicing all the samples you have.
    Creating WCF Rest service hosting on IIS and consuming in WPF. I am problem with data access layer, I need use ADO.net and stored procedure to display data on WPF grid. Any articles that I can use as example.
    Thank you
    Indu

     
    • nazmulababy

      March 24, 2011 at 9:35 pm

      Indu, if your DAL is fully encapsulated within your service, it should not have any impact on either the client or the transport. and that would make ADO or stored procedure simply implementation details. However, I’ll add a sample on the blog to send collections, derived from database records, over the wire. . .over the next few days.

       
      • Suneetha

        March 25, 2011 at 12:51 pm

        Thank you I will look forward to that.
        I appreciate your help, its your examples that is helped it now.

        Thanks
        Indu

         

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.