How to: Create a Client


Updated: October 9, 2012

Applies To: Windows Server 2012 Essentials, Windows Home Server 2011, Windows Storage Server 2008 R2 Essentials, Windows Small Business Server 2011 Essentials

The client application is not required for a provider; however, it is included to represent how the operations of the provider can be used. This client is a simple graphical interface that communicates with the provider through the ChatObjectModel object.

Before you start the procedures in this section, you should complete the procedures listed in How to: Create a Host Process.

To create the client application

  1. Right-click the MyProvider solution, click Add, and then click New Project.

  2. In the Templates pane, click WPF Application, type ChatWindow in the Name text box, and then click OK.

  3. Add a reference to the ChatSample.ObjectModel project.

  4. Add a reference to the WssgCommon.dll file.

  5. Open the MainWindow.xaml file and make sure that it contains the following code.

    <Window x:Class="ChatWindow.MainWindow"  
       xmlns:mc="" mc:Ignorable="d"  
       Title="Window1" Height="350" Width="525"   
            <TextBox HorizontalAlignment="Left" Margin="81.183,8,0,0" x:Name="UserName"  
             VerticalAlignment="Top" Text="<enter user name>" TextWrapping="Wrap"  
            <Label HorizontalAlignment="Left" Margin="8,6,0,0" VerticalAlignment="Top"   
             Content="User name:"/>  
            <Button d:LayoutOverrides="Width" HorizontalAlignment="Right" Margin="0,8,8,0"  
             x:Name="ConnectButton" VerticalAlignment="Top" Content="Connect"   
            <ListBox IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Right"   
             Margin="0,47,8,8" x:Name="CurrentUsers" Width="154"/>  
            <TextBox Margin="8,47,166,33.96" Text="" TextWrapping="Wrap" x:Name="ChatText"/>  
            <TextBox d:LayoutOverrides="Height" Margin="8,0,202.507,8"   
             VerticalAlignment="Bottom" Text="<enter chat text>" TextWrapping="Wrap"   
             x:Name="ChatEntry" IsEnabled="False"/>  
            <Button d:LayoutOverrides="Width, Height" HorizontalAlignment="Right"   
             Margin="0,0,166,8" VerticalAlignment="Bottom" Content="Send" x:Name="Send"  
             Click="Send_Click" IsDefault="True" IsEnabled="False"/>  
  6. Save the file.

  7. Open the MainWindow.xaml.cs file and make sure that it contains the following code.

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using System.Windows;  
    using System.Windows.Controls;  
    using System.Windows.Data;  
    using System.Windows.Documents;  
    using System.Windows.Input;  
    using System.Windows.Media;  
    using System.Windows.Media.Imaging;  
    using System.Windows.Navigation;  
    using System.Windows.Shapes;  
    using System.Windows.Threading;  
    using System.ComponentModel;  
    using ChatSample.ObjectModel;  
    namespace ChatWindow {  
        public partial class MainWindow : Window  
          public MainWindow() {  
          private void ConnectButton_Click(object sender, RoutedEventArgs e) {  
             if (this.DataContext == null)  
                    this.DataContext = new ObjectModel();  
             ObjectModel objModel = (ObjectModel)this.DataContext;  
             objModel.ResponseReceived += ChatReceived;  
             objModel.PropertyChanged += objModel_PropertyChanged;  
             this.ConnectButton.IsEnabled = false;  
             this.UserName.IsEnabled = false;  
          void objModel_PropertyChanged(object sender, PropertyChangedEventArgs e) {  
             if (e.PropertyName == "Connected") {  
                   new Action(() => this.ConnectedChanged()));  
          private void ConnectedChanged() {  
             ObjectModel objModel = this.DataContext as ObjectModel;  
             bool connected = objModel.Connected;  
             if (connected) {  
                this.UserName.IsReadOnly = true;  
                this.ChatEntry.IsEnabled = true;  
                this.Send.IsEnabled = true;  
             } else {  
                this.ChatEntry.IsEnabled = false;  
                this.Send.IsEnabled = false;  
                this.ChatText.Text = this.ChatText.Text + "<Disconnected>"  
                   + Environment.NewLine;  
                this.ConnectButton.IsEnabled = true;  
          private void ChatReceived(object sender, ChatReceivedEventArgs e) {  
             string formattedLine = String.Format("[{0}] {1}{2}",  
                e.User, e.Text, Environment.NewLine);  
             this.ChatText.Text = this.ChatText.Text + formattedLine;  
          private void Send_Click(object sender, RoutedEventArgs e) {  
             ObjectModel objModel = this.DataContext as ObjectModel;  
  8. Save the project and build the solution.

Community Additions