学习参考《C#网络应用编程》(第3版)马骏主编的教科书,以及老师教学课件。写这篇是因为具体实现跟书上不完全一样,有点点懵,同时,也是个比较难的步骤,写来码一下~
首先新建WPF应用程序,以创建客户端,然后创建服务端,步骤为:
右键单击“解决方案’WcfServiceExamples‘(1个项目)——添加——新建项目~~(书上说是新建项,但是根据后续来看新建项目里面的更符合本次创建,WCF服务应用程序只在新建项目里,新建项没有该组件,如果有大佬知道为什么,欢迎点明)~
左侧找到 已安装——Visual C#——WCF,点击WCF右边就会出现WCF服务应用程序(PS:如果没有,很大可能是一个组件没装)
(在IService1.cs中添加以上代码内容)
(在IService1.svc.cs中添加以上代码)
4、客户段编码
(1)前端设计
MainWindow.xmal中代码为:
<Window x:Class="Client.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Client"
mc:Ignorable="d"
Title="WCF客户端示例" Height="330" Width="600" Background="#FFF0F9D8" WindowStartupLocation="CenterScreen">
<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Rectangle Grid.ColumnSpan="2" Fill="white" RadiusX="14"
RadiusY="14" Stroke="Blue" StrokeDashArray="3" />
<Rectangle Grid.Column="0" Margin="7" Fill="#FFF0F9D8" RadiusX="10"
RadiusY="10" Stroke="Blue" StrokeDashArray="3" />
<Rectangle Grid.Column="0" Margin="20" Fill="White" Stroke="Blue" />
<ScrollViewer Grid.Column="0" Margin="22"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Visible">
<StackPanel>
<StackPanel.Resources>
<Style TargetType="Button">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Margin" Value="5 10 5 0" />
<Setter Property="FontSize" Value="10" />
<EventSetter Event="Click" Handler="button_Click" />
</Style>
</StackPanel.Resources>
<Button Content="例1" Tag="Examples/Page1.xaml" />
<Button Content="例2" Tag="Examples/Page2.xaml" />
<Button Content="例3" Tag="Examples/Page3.xaml" />
</StackPanel>
</ScrollViewer>
<Frame Name="frame1" Grid.Column="1" Margin="10" BorderThickness="1" BorderBrush="Blue" NavigationUIVisibility="Hidden" />
</Grid>
</Window>
在MainWindow.xaml.cs中写入:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
namespace Client
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private Button oldButton = new Button();
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
Button btn = e.Source as Button;
oldButton.Foreground = Brushes.Black;
btn.Foreground = Brushes.Red;
oldButton = btn;
frame1.Source = new Uri(btn.Tag.ToString(), UriKind.Relative);
}
}
}
(2)新建Examples文件夹,添加名为Page1的页,在xaml中写入
<Page x:Class="Client.Examples.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Client.Examples"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1">
<DockPanel>
<TextBlock DockPanel.Dock="Top" Style="{StaticResource TitleStyle}" Text="basicHttpBinding的入门示例1-服务协定"/>
<Separator DockPanel.Dock="Top"/>
<Border DockPanel.Dock="Bottom" Background="AliceBlue">
<StackPanel>
<StackPanel Margin="10" Orientation="Horizontal" HorizontalAlignment="Center" Height="35">
<Button Name="btn1" Width="100" Content="调用SayHello" Click="btn1_Click"/>
<Button Name="btn2" Margin="20 0 0 0" Width="110" Content="调用多个服务操作" Click="btn2_Click"/>
</StackPanel>
</StackPanel>
</Border>
<ScrollViewer>
<StackPanel Background="White" TextBlock.LineHeight="20">
<TextBlock x:Name="textBlock1" Margin="5" TextWrapping="Wrap"/>
</StackPanel>
</ScrollViewer>
</DockPanel>
</Page>
在xaml.cs中写入
using Client.ServiceReference1;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
namespace Client.Examples
{
/// <summary>
/// Page1.xaml 的交互逻辑
/// </summary>
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
textBlock1.Text = "客户端调用服务端的SayHello方法,服务端返回:\n";
//创建服务客户端
Service1Client client = new Service1Client();
//调用服务
string s = client.SayHello("欢迎学习WCF!");
//关闭服务客户端并清理资源
client.Close();
textBlock1.Text += s;
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
textBlock1.Text += "\n\n客户端调用服务端的多个方法,服务端返回:";
Service1Client client = new Service1Client();
string s = client.SayHello(client.Endpoint.Address.ToString());
double r1 = client.Add(10, 20);
double r2 = client.Divide(10, 20);
client.Close();
textBlock1.Text += string.Format("\n{0}", s);
textBlock1.Text += string.Format("\n10 + 20 = {0}", r1);
textBlock1.Text += string.Format("\n10 / 20 = {0}", r2);
}
}
}
成果为: