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

<Window x:Class="RoomReservationClient.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Room Reservation" Height="300" Width="365">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        
        <Label Grid.Column="0" Grid.Row="0" Margin="5">Room:</Label>
        <Label Grid.Column="0" Grid.Row="1" Margin="5">Begin Time:</Label>
        <Label Grid.Column="0" Grid.Row="2" Margin="5">End Time:</Label>
        <Label Grid.Column="0" Grid.Row="3" Margin="5">Contact:</Label>
        <Label Grid.Column="0" Grid.Row="4" Margin="5">Event:</Label>
        
        <TextBox Grid.Column="1" Grid.Row="0" Margin="10" Text="{Binding RoomName}" />
        <TextBox Grid.Column="1" Grid.Row="1" Margin="10" Text="{Binding StartTime}" />
        <TextBox Grid.Column="1" Grid.Row="2" Margin="10" Text="{Binding EndTime}" />
        <TextBox Grid.Column="1" Grid.Row="3" Margin="10" Text="{Binding Contact}" />
        <TextBox Grid.Column="1" Grid.Row="4" Margin="10" Text="{Binding Event}" />
        
        <Button x:Name="ReserveButton" Grid.Row="5" Margin="10" Click="OnReserveRoom" Grid.ColumnSpan="2">Reserve Room</Button>
        
    </Grid>
</Window>

  • Modify the MainWindow.xaml.cs as below
using System;
using System.Windows;
using RoomReservationClient.RoomReservationService;
using RoomReservationContracts;
 
namespace RoomReservationClient
{
    public partial class MainWindow : Window
    {
        private RoomReservation reservation;
 
        public MainWindow()
        {
            InitializeComponent();
 
            reservation = new RoomReservation {
                ID = 3,
                StartTime = DateTime.Now,
                EndTime = DateTime.Now.AddHours(1)
            };
 
            this.DataContext = reservation;
        }
 
        private async void OnReserveRoom(object sender, RoutedEventArgs e)
        {
            var client = new RoomServiceClient();
            bool reserved = await client.ReserveRoomAsync(reservation);
 
            client.Close();
 
            if (reserved)
                MessageBox.Show("Reservation has Done!");
        }
 
    }
}
  • 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:
using System;
using System.Windows;
using System.ServiceModel;
using RoomReservationContracts;
 
namespace RoomReservationClientSharedAssembly
{
    public partial class MainWindow : Window
    {
        private RoomReservation reservation;
 
        public MainWindow()
        {
            InitializeComponent();
 
            reservation = new RoomReservation
            {
                ID = 3,
                StartTime = DateTime.Now,
                EndTime = DateTime.Now.AddHours(1)
            };
 
            this.DataContext = reservation;
        }
 
        private void OnReserveRoom(object sender, RoutedEventArgs e)
        {
            var binding = new BasicHttpBinding();
            var address = new EndpointAddress("http://localhost:8733/Design_Time_Addresses/RoomReservationservice/RoomReservationService");
 
            var factory = new ChannelFactory<IRoomService>(binding, address);
            IRoomService channel = factory.CreateChannel();
 
            if (channel.ReserveRoom(reservation))
            {
                MessageBox.Show("Reservation has Done!");
            }
        }
    }
}

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.