How to call wcf service from wpf client


DataBaseDesign:

First create database with name MySampleDB and create table as follows with name UserDetails

then add data to table as follows:

WCFService Creation:

First go to file newproject–>wcfservicelibrary

Then add required service files as follows

1. interface class

2.Class Implementing InterFace

3.UserDefinedClasses(if any used)

Write following code in Userdefined Class:

Write this code in Interface Class:

Then Write Following Code in Class implementing Interface

After Completion of creating service run the application

now service will hosted check service is hosted of no as follows

After completion of service hosting you can get the following window

To check wether your service is running or not call the service methods in

wcftestclient window as follows

Now your service is Hosted successfully.

Now prepare client as follows

WPFClient:

Open new visualstudio go to file–> new Project–>wpfclient

Design wpfclient as follows

This is the code for above window design

  1. <Window x:Class="WpfClient.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
  4. Title="MainWindow" Height="500" Width="800">
  5.     <Grid>
  6.         <GroupBox Header="Details" Height="361" HorizontalAlignment="Left" Margin="500,27,0,0" Name="groupBox1" VerticalAlignment="Top" Width="266">
  7.             <Grid>
  8.                 <Label Content="EnterID:" Height="28" HorizontalAlignment="Left" Margin="16,17,0,0" Name="label1" VerticalAlignment="Top" FontWeight="Bold" FontSize="16" />
  9.                 <Label Content="UserID:" FontSize="16" FontWeight="Bold" Height="28" HorizontalAlignment="Left" Margin="16,122,0,0" Name="label2" VerticalAlignment="Top" />
  10.                 <Label Content="UserName:" FontSize="16" FontWeight="Bold" Height="28" HorizontalAlignment="Left" Margin="16,162,0,0" Name="label3" VerticalAlignment="Top" />
  11.                 <Label Content="LastName:" FontSize="16" FontWeight="Bold" Height="28" HorizontalAlignment="Left" Margin="16,204,0,0" Name="label4" VerticalAlignment="Top" />
  12.                 <Label Content="Location:" FontSize="16" FontWeight="Bold" Height="28" HorizontalAlignment="Left" Margin="16,244,0,0" Name="label5" VerticalAlignment="Top" />
  13.                 <TextBox Height="23" HorizontalAlignment="Left" Margin="101,22,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
  14.                 <TextBox Height="23" HorizontalAlignment="Left" Margin="118,167,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
  15.                 <TextBox Height="23" HorizontalAlignment="Left" Margin="118,127,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />
  16.                 <TextBox Height="23" HorizontalAlignment="Left" Margin="118,209,0,0" Name="textBox4" VerticalAlignment="Top" Width="120" />
  17.                 <TextBox Height="23" HorizontalAlignment="Left" Margin="118,249,0,0" Name="textBox5" VerticalAlignment="Top" Width="120" />
  18.                 <Button Content="GetSingleRecord" Height="36" HorizontalAlignment="Left" Margin="25,66,0,0" Name="button1" VerticalAlignment="Top" Width="196" Click="button1_Click_1" />
  19.             </Grid>
  20.         </GroupBox>
  21.         <GroupBox Header="AllDetails" Height="361" HorizontalAlignment="Left" Margin="22,27,0,0" Name="groupBox2" VerticalAlignment="Top" Width="453">
  22.             <Grid>
  23.                 <DataGrid AutoGenerateColumns="False" Height="263" HorizontalAlignment="Left" Margin="14,73,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="413" AlternatingRowBackground="#FFDEA5DE" RowBackground="#FFFFBEA5" FontSize="18" FontWeight="Bold">
  24.                     <DataGrid.Columns>
  25.                         <DataGridTemplateColumn Header="UserId" Width="SizeToCells" IsReadOnly="True">
  26.                             <DataGridTemplateColumn.CellTemplate>
  27.                                 <DataTemplate>
  28.                                     <TextBlock Text="{Binding Path=UserId}"></TextBlock>
  29.                                 </DataTemplate>
  30.                             </DataGridTemplateColumn.CellTemplate>
  31.                         </DataGridTemplateColumn>
  32.                         <DataGridTemplateColumn Header="UserId" Width="SizeToCells" IsReadOnly="True">
  33.                             <DataGridTemplateColumn.CellTemplate>
  34.                                 <DataTemplate>
  35.                                     <TextBlock Text="{Binding Path=UserName}"></TextBlock>
  36.                                 </DataTemplate>
  37.                             </DataGridTemplateColumn.CellTemplate>
  38.                         </DataGridTemplateColumn>
  39.                         <DataGridTemplateColumn Header="UserId" Width="SizeToCells" IsReadOnly="True">
  40.                             <DataGridTemplateColumn.CellTemplate>
  41.                                 <DataTemplate>
  42.                                     <TextBlock Text="{Binding Path=LastName}"></TextBlock>
  43.                                 </DataTemplate>
  44.                             </DataGridTemplateColumn.CellTemplate>
  45.                         </DataGridTemplateColumn>
  46.                         <DataGridTemplateColumn Header="UserId" Width="SizeToCells" IsReadOnly="True">
  47.                             <DataGridTemplateColumn.CellTemplate>
  48.                                 <DataTemplate>
  49.                                     <TextBlock Text="{Binding Path=Location}"></TextBlock>
  50.                                 </DataTemplate>
  51.                             </DataGridTemplateColumn.CellTemplate>
  52.                         </DataGridTemplateColumn>
  53.                     </DataGrid.Columns>
  54.                 </DataGrid>
  55.                 <Button Content="GetDataFromDataSet" Height="37" HorizontalAlignment="Left" Margin="14,22,0,0" Name="button2" VerticalAlignment="Top" Width="183" Click="button2_Click_1" />
  56.                 <Button Content="GetDataFromUserDefinedType" Height="39" HorizontalAlignment="Left" Margin="241,20,0,0" Name="button3" VerticalAlignment="Top" Width="186" Click="button3_Click_2" />
  57.             </Grid>
  58.         </GroupBox>
  59.     </Grid>
  60. </Window>

After Completion of designing we need to Create Proxy for service to consume service:

To create Proxy Do the following:

First Copy The Address Of  Service:

Then Add System.ServiceModel  dll file from .net tab of add reference

Then RightClick on your Project select Add ServiceReference:

After Adding this proxy if you want to see things added to servicereference

first select project the click on showhidden files as shown in below diagram

Then Write Following Code In CodeBehind:

Then Satart Calling From Client:

DownloadSampleCode

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s