WCF总结

前言

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这是一个很容易理解框架。但是需要多联系,看视频的时候并不能理解里面都有那些内容,都是做什么的,敲了一个例子之后大概就都理解了 。
感谢您的阅读~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 19
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值