前言
wcf是一种面向服务的架构,在学习wcf之前需要了解面向服务架构(SOA)对SOA的理解就是一组公开开发的接口,并且提供查询的组件。
SOA有四个特性
1.每个服务具有明确的边界
2.服务是独立的
3.采用标准的契约定义和通信协议
4.服务都是自解释的
内容
什么是wcf
wcf全称Windows通信基础Windows communication foundation 是一组软件开发包
wcf架构组成
契约
服务运行
消息
宿主
wcf应用的例子
服务端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.ServiceModel;
namespace WcfServiceDE
{
[ServiceContract]
public interface IHelloService
{
[OperationContract ]
DateTime getDataTime();
[OperationContract]
DataTable getUserInfo();
[OperationContract(Name = "HelloString")]
string Hello(string name);
[OperationContract(IsOneWay = true, Name = "HelloVoid")]
void Hello();
}
}
客户端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WcfServiceDE;
using System.ServiceModel;
using System.Data.SqlClient;
using System.Data;
namespace WinformsHello
{
public class HelloService: IHelloService
{
public DateTime getDateTime()
{
return DateTime.Now;
}
public System.Data.DataTable getUserInfo()
{
string connstring = @"data source = .;Initial Catalog=companydb; Persist Security Info = Ture ; User ID =sa ;Pwd =123456 ";
SqlConnection conn = new SqlConnection(connstring );
SqlDataAdapter sda = new SqlDataAdapter("select*from tbUser", conn);
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
public void Hello()
{
throw new NotImplementedException();
}
}
}
窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ServiceModel;
namespace WinformsHello
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ServiceHost host = null;
private void button1_Click(object sender, EventArgs e)
{
host = new ServiceHost(typeof(WinformsHello.HelloService ));
host.Open();
label1.Text = "服务已启动";
}
}
}
小结
学习wcf这是一个很容易理解框架。但是需要多联系,看视频的时候并不能理解里面都有那些内容,都是做什么的,敲了一个例子之后大概就都理解了 。
感谢您的阅读~~