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:
- 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
- Reference the System.ServiceModel.Web library to use the WebChannelFactory
- 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.
Indu said:
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 said:
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 said:
Thank you I will look forward to that.
I appreciate your help, its your examples that is helped it now.
Thanks
Indu
consulting Internet Marketing services said:
Superb post however , I was wanting to know
if you could write a litte more on this topic? I’d be very thankful if you could elaborate a little bit further. Thanks!
Sacramento bankruptcy lawyers said:
Why users still make use of to read news papers when
in this technological world all is accessible on net?