1,创建wcf项目
2,删掉默认的wcf服务
3,创建自己的wcf服务名称
4,编写wcf服务
在Icalculator接口中写一些方法,一定要写上
[OperationContract]
,如若不写外界无法对其进行调用。
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfService1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“Icalculator”。
//[ServiceContract]来说明接口是一个WCF的接口,如果不加的话,将不能被外部调用。
[ServiceContract]
public interface Icalculator
{
//[OperationContract]来说明该方法是一个WCF接口的方法,不加的话,将不能被外部调用。
[OperationContract]
double Add(double x, double y);
[OperationContract]
double Subtract(double x,double y);
[OperationContract]
DataTable ShowActivity();
[OperationContract]
string Msg();
[OperationContract]
string Time();
}
}
在calculator.svc.cs实现接口
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfService1
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“calculator”。
// 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 calculator.svc 或 calculator.svc.cs,然后开始调试。
public class calculator : Icalculator
{
public double Add(double x, double y)
{
return x + y;
}
public string Msg()
{
return "生日快乐";
}
public DataTable ShowActivity()
{
DataSet ds = new DataSet();
SqlConnection coon = new SqlConnection("Data Source=.;Initial Catalog=数据库名;User id=账号;password=密码");
string sql = "select * from 表";
using (SqlCommand cmd = new SqlCommand(sql,coon))
{
coon.Open();
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
return ds.Tables[0];
}
public double Subtract(double x, double y)
{
return x - y;
}
public string Time()
{
return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
}
}
5,对WCF服务接口测试看看是否无误。
选中calculator.svc右键在浏览器中查看,然后复制其路径。
6,调用WCF接口
新建一个客户端项目,引用-添加服务引用,地址填上面复制的路径
添加成功后,会出现如下图所示
然后实例化接口
ServiceReference1.IcalculatorClient icalculator = new IcalculatorClient();
贴代码
using NpoiDemo.ServiceReference1;
using System;
using System.Collections.Generic;
using System.Data;
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.Shapes;
namespace NpoiDemo
{
/// <summary>
/// WPFtest.xaml 的交互逻辑
/// </summary>
public partial class WPFtest : Window
{
List<Activity> listActivity = new List<Activity>();
ServiceReference1.IcalculatorClient icalculator = new IcalculatorClient(); //实例化wcf接口
public WPFtest()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
DataTable dt = icalculator.ShowActivity();
foreach (DataRow dr in dt.Rows)
{
Activity activity = new Activity();
activity.ActivityCode = dr["ActivityCode"].ToString();
activity.ActivityContent = dr["ActivityContent"].ToString();
activity.StartTime = dr["StartTime"].ToString();
activity.EndTime = dr["EndTime"].ToString();
activity.Remark = dr["Remark"].ToString();
listActivity.Add(activity);
}
data.ItemsSource = listActivity;
}
private void btnCalculate_Click(object sender, RoutedEventArgs e)
{
if (tbx1.Text !="" && tbx2.Text !="")
{
double a;
double.TryParse(tbx1.Text, out a);
double b;
double.TryParse(tbx2.Text, out b);
tbx1_2.Text = icalculator.Add(a, b).ToString();
}
if (tbx3.Text != "" && tbx4.Text != "")
{
double c;
double.TryParse(tbx3.Text, out c);
double d;
double.TryParse(tbx4.Text, out d);
tbx3_4.Text = icalculator.Subtract(c, d).ToString();
}
}
private void btnSurprise_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(icalculator.Time());
}
}
public class Activity
{
public string ActivityCode { get; set; }
public string ActivityContent { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
public string Remark { get; set; }
}
}
xaml代码
<Window x:Class="NpoiDemo.WPFtest"
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:NpoiDemo"
mc:Ignorable="d"
Title="WPFtest" Height="600" Width="800" Loaded="Window_Loaded">
<Grid>
<StackPanel>
<DataGrid x:Name="data" AutoGenerateColumns="False" Height="450" VerticalAlignment="Top">
<DataGrid.Columns>
<DataGridTextColumn Header="编号" Binding="{Binding ActivityCode}"></DataGridTextColumn>
<DataGridTextColumn Header="活动类容" Binding="{Binding ActivityContent}"></DataGridTextColumn>
<DataGridTextColumn Header="开始时间" Binding="{Binding StartTime}"></DataGridTextColumn>
<DataGridTextColumn Header="结束时间" Binding="{Binding EndTime}"></DataGridTextColumn>
<DataGridTextColumn Header="备注" Binding="{Binding Remark}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<StackPanel Orientation="Horizontal">
<TextBox Width="50" Height="50" Margin="10 10 0 0" Name="tbx1" VerticalContentAlignment="Center"></TextBox>
<TextBlock VerticalAlignment="Center" FontSize="20" Margin="10 10 0 0">+</TextBlock>
<TextBox Width="50" Height="50" Margin="10 10 0 0" Name="tbx2" VerticalContentAlignment="Center"></TextBox>
<TextBlock VerticalAlignment="Center" FontSize="20" Margin="10 10 0 0">=</TextBlock>
<TextBox Width="50" Height="50" Margin="10 10 0 0" Name="tbx1_2" VerticalContentAlignment="Center"></TextBox>
<TextBox Width="50" Height="50" Margin="80 10 0 0" Name="tbx3" VerticalContentAlignment="Center"></TextBox>
<TextBlock VerticalAlignment="Center" FontSize="20" Margin="10 10 0 0">-</TextBlock>
<TextBox Width="50" Height="50" Margin="10 10 0 0" Name="tbx4" VerticalContentAlignment="Center"></TextBox>
<TextBlock VerticalAlignment="Center" FontSize="20" Margin="10 10 0 0">=</TextBlock>
<TextBox Width="50" Height="50" Margin="10 10 0 0" Name="tbx3_4" VerticalContentAlignment="Center"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button Width="100" Height="30" Margin="220 20 0 0" x:Name="btnCalculate" Click="btnCalculate_Click">计算</Button>
<Button Width="100" Height="30" Margin="250 20 0 0" x:Name="btnSurprise" Click="btnSurprise_Click">惊喜</Button>
</StackPanel>
</StackPanel>
</Grid>
</Window>
引用wcf接口后运行效果图