We have our WCF Service perfectly working, but how we can use it from other projects/applications? Here I’ll show how to refer and use a WCF Service methods. It will be easy! 😉

In this post we have seen how to create a WCF Service and test it. Now, we are going to use it from another project.

After we create a new WPF Application (I have called it callingWCFfromWPF), we have to import the reference of our WCF Service.

In the Solution Explorer of Visual Studio, right-click on References and click on “Add service reference”. Insert the URL of the service (the temporary generated by Visual Studio when launches the service or the one you have set with IIS) and click Go. In the Services box (on the the left) you can select the service. Then, click ok. With the authentication enabled, it will asked for the username and password (depending on how you set IIS options, it can be ask the username and password of the App.confing file of the service or the credentials of a Windows User of the server).

After this, you have a reference to the service, like an offiline classic dll.  NOTE: if the parameter of the service will change, you have to right-click on the service and click on “Update service reference”.

This how you have to set the xaml of your winwow (in a new project, MainWindow.xaml):


<Window x:Class="callingWCFfromWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>

<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Height="23" Text="" VerticalAlignment="Center" HorizontalAlignment="Center" Width="120" Name="opOne"/>
<TextBox Grid.Column="1" Height="23" Text="" VerticalAlignment="Center" HorizontalAlignment="Center" Width="120" Name="opTwo"/>
<TextBox Grid.Column="2" Height="23" Text="" VerticalAlignment="Center" HorizontalAlignment="Center" Width="120" Name="res"/>
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Content="ADD" Height="23" Width="120" VerticalAlignment="Center" HorizontalAlignment="Center" Click="Add"/>
<Button Grid.Column="1" Content="SUB" Height="23" Width="120" VerticalAlignment="Center" HorizontalAlignment="Center" Click="Sub"/>
<Button Grid.Column="2" Content="MULTIPLY" Height="23" Width="120" VerticalAlignment="Center" HorizontalAlignment="Center" Click="Mul"/>
<Button Grid.Column="3" Content="DIVIDE" Height="23" Width="120" VerticalAlignment="Center" HorizontalAlignment="Center" Click="Div"/>
</Grid>
</Grid>
</Window>

In the MainWindow.xaml.cs file, in the MainWindow class, write these methods:


private void Add(object sender, RoutedEventArgs e)
{
var n1 = this.opOne.Text.ToString();
var n2 = this.opTwo.Text.ToString();
var calc = new CardServiceLib.CardServiceClient();
calc.ClientCredentials.UserName.UserName = username;
calc.ClientCredentials.UserName.Password = pwd;
try
{
this.res.Text = calc.Add(n1, n2);
}
catch (MessageSecurityException ex)
{
MessageBox.Show("Wrong username/password!", "Login Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
catch (Exception ex)
{
MessageBox.Show("Generic Error!", "Login Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}

private void Sub(object sender, RoutedEventArgs e)
{
var n1 = this.opOne.Text.ToString();
var n2 = this.opTwo.Text.ToString();
var calc = new CardServiceLib.CardServiceClient();
calc.ClientCredentials.UserName.UserName = username;
calc.ClientCredentials.UserName.Password = pwd;
try
{
this.res.Text = calc.Subtract(n1, n2);
}
catch (MessageSecurityException ex)
{
MessageBox.Show("Wrong username/password!", "Login Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
catch (Exception ex)
{
MessageBox.Show("Generic Error!", "Login Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}

private void Mul(object sender, RoutedEventArgs e)
{
var n1 = this.opOne.Text.ToString();
var n2 = this.opTwo.Text.ToString();
var calc = new CardServiceLib.CardServiceClient();
calc.ClientCredentials.UserName.UserName = username;
calc.ClientCredentials.UserName.Password = pwd;
try
{
this.res.Text = calc.Multiply(n1, n2);
}
catch (MessageSecurityException ex)
{
MessageBox.Show("Wrong username/password!", "Login Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
catch (Exception ex)
{
MessageBox.Show("Generic Error!", "Login Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}

private void Div(object sender, RoutedEventArgs e)
{
var n1 = this.opOne.Text.ToString();
var n2 = this.opTwo.Text.ToString();
var calc = new CardServiceLib.CardServiceClient();
calc.ClientCredentials.UserName.UserName = username;
calc.ClientCredentials.UserName.Password = pwd;
try
{
this.res.Text = calc.Divide(n1, n2);
}
catch (MessageSecurityException ex)
{
MessageBox.Show("Wrong username/password!", "Login Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
catch (Exception ex)
{
MessageBox.Show("Generic Error!", "Login Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}

For the authentication, I have written the value of username and password in the client App.config file, but you can do how you prefer! 😉 it’s almost the same.

This is a classic and simple calculator app, but the function are calling a WCF Service, protected with Username and password (better than nothing ^_^ ).

Advertisements