WCF Tutorial, Part 7: Create WCF Client

01. December 2013 WCF 0

The client application can be of any type from a simple console application to a WPF application. We will use WPF application for demonstration.

  • Add a new WPF Application to the solution and name it as RoomReservationClient
  • Start the service and add the service reference to this project with the name RoomReservationService

    Doing so will be automatically adding System.Runtime.Serialization and System.ServiceModel references to the project.

    The endpoint information will be added to the App.config as well.

  • Modify the MainWindow.xaml as bellow:

MainWindow.xaml

  • Modify the MainWindow.xaml.cs as below

  • Run and test the application
  • The await operator is applied to a task in an asynchronous method to suspend the execution of the method until the awaited task completes. The task represents ongoing work.
  • The asynchronous method in which await is used must be modified by the async keyword. Such a method, defined by using the async modifier, and usually containing one or more await expressions, is referred to as an async method.

Using Shared Contract Assembly

  • Instead of adding the service reference to the WPF project the ChannelFactory<TChannel> class can be used to instantiate the channel to connect to the service.
  • The constructor of the class ChannelFactory<TChannel> accepts the binding configuration and endpoint address. The binding must be compatible with the binding defined with the service host, and the address defined with the EndpointAddress class references the URI of the running service. The CreateChannel method creates a channel to connect to the service. Then, you can invoke methods of the service.

  • Add a reference to the RoomReservationContract.
  • Modify the MainWindow.xaml.cs as bellow:


Leave a Reply

Your email address will not be published. Required fields are marked *