那天有个小孩教我WCF[一][2/3]
接着上次的继续讲吧
我们开始吧
9.创建数据库
use mastergo--创建库if exists(select * from sysdatabases where name='NewsDB')drop database NewsDBcreate database NewsDBon primary(name='NewsDB_data',filename='D:\NewsDB_data.mdf',filegrowth=30%,size=5)log on(name='NewsDB_log',filename='D:\NewsDB_log.ldf',size=2,filegrowth=10%)go--创建表Newsuse NewsDBgoif exists(select * from sysobjects where name='News')drop table Newscreate table News(NewsID int identity(1,1) primary key,NewsTitle nvarchar(50) not null,Content nvarchar(max) not null,NewsType int not null,publishTime datetime not null,LastUpdateTime datetime not null,Author nvarchar(20) not null,LastAuthor nvarchar(20) not null,ReadCount int not null)
10. 添加Linq to SQL文件
11. 实现服务
11.1 修改NewsTypeEnum枚举文件
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.Serialization;namespace NewsInterface{[DataContract]public enum NewsTypeEnum{[EnumMember]Sports=1,[EnumMember]IT=2,[EnumMember]Country=3,[EnumMember]Funny=4}}
11.2 在D盘放一张 1.jpg图片作为测试用
1.jpg
11.3 打开NewsImpl.cs文件,实现服务
修改NewsDto,添加NewsID属性
NewsImpl.cs代码实现大致如下
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.Serialization;using System.ServiceModel;using NewsInterface;using System.IO;namespace NewsServices{public class NewsImpl:INewsInterface{public int NewsAdd(NewsDto dto){Console.WriteLine("添加中...");using (NewsDataContext db=new NewsDataContext()){News news = new News();news.Author=dto.Author;news.Content=dto.Content;news.LastAuthor=dto.LastAuthor;news.LastUpdateTime=dto.LastUpdateTime;news.NewsType=dto.NewsType.GetHashCode();news.publishTime = dto.publishTime;news.ReadCount = dto.ReadCount;news.NewsTitle = dto.NewsTitle;db.News.InsertOnSubmit(news);db.SubmitChanges();Console.WriteLine("添加成功!");return news.NewsID;}}public bool NewsDelete(NewsDto dto){Console.WriteLine("删除中...");using (NewsDataContext db = new NewsDataContext()){try{News model = db.News.FirstOrDefault(x => x.NewsID == dto.NewsID);db.News.DeleteOnSubmit(model);db.SubmitChanges();Console.WriteLine("删除成功");return true;}catch{return false;}}}public bool NewsUpdate(NewsDto dto){throw new NotImplementedException();}public List<NewsDto> NewsList(){Console.WriteLine("请求获得新闻列表");using (NewsDataContext db = new NewsDataContext()){var d=from o in db.Newsselect new NewsDto{Author=o.Author,LastAuthor=o.LastAuthor,LastUpdateTime=o.LastUpdateTime,NewsID=o.NewsID,Content=o.Content,NewsTitle=o.NewsTitle,publishTime=o.publishTime,ReadCount=o.ReadCount};return d.ToList<NewsDto>();}}public byte[] GetNewsImage(string Id){Console.WriteLine("开始获得图片:"+Id+"...");byte[] buff;string path = @"D:\1.jpg";FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);BinaryReader br = new BinaryReader(fileStream);buff = br.ReadBytes((int)fileStream.Length);Console.WriteLine("获取成功!");return buff;}}}
这里修改我没有去实现,感兴趣自己可以实现
LINQ to SQL如果不会的,可以 点击学习 那天有个小孩跟我说LINQ
11.4 重新生成解决方案,编译程序
12. 公开元数据
现在可以通过添加配置来托管服务了,并公开元数据。公开元数据之后,Visual Studio就可以下载WSDL文件,创建所需要的代理了。我们后期会教大家创建代理的其他方式
现在用WCF配置编辑器打开NewsHosts应用程序的配置文件App.config
新建服务行为的名称 ExposeMetaDataBehavior
元数据发布地址 http://localhost:1234/NewsService/Mex
下面具体配置一下
配置完成后App.config
配置成功后,将HOST设为启动项目,运行项目,打开浏览器输入
http://localhost:1234/NewsService/Mex
效果如果是这样子的,说明 成功的
13. 创建 NewsApplication客户端,我们使用Winform
窗体,我改成这样子了,上面的蓝色文字用于说明
最重要的一步,添加服务
先不用VS运行宿主
我们手动打开Debug文件夹下的 NewsHosts.exe
接下来看我演示一下
我们最后注释掉 identity节点,是为了方便测试
14 实现Winform后面的方法
14.1 添加
private void button1_Click(object sender, EventArgs e){try{NewsService.NewsInterfaceClient client = new NewsService.NewsInterfaceClient();NewsService.NewsDto dto = new NewsService.NewsDto();dto.Author = "杨洋";dto.LastAuthor = "AaronYang";dto.publishTime = DateTime.Now;dto.LastUpdateTime = DateTime.Now;dto.NewsTitle = "测试标题:" + new Random().Next(10000).ToString();dto.Content = "测试内容" + new Random().Next(10000).ToString();dto.NewsType = NewsService.NewsTypeEnum.Country;dto.ReadCount = 0;MessageBox.Show("添加成功!成功后的ID为"+client.NewsAdd(dto).ToString());}catch (Exception ex){richTextBox1.Text += Environment.NewLine + ex.Message;}}
14.2 获得新闻列表
private void button2_Click(object sender, EventArgs e){try{NewsService.NewsInterfaceClient client = new NewsService.NewsInterfaceClient();List<NewsService.NewsDto> list = client.NewsList();foreach (NewsService.NewsDto item in list){listBox1.Items.Add("标题:"+item.NewsTitle+"\t发布时间:"+item.publishTime.ToShortDateString());}}catch (Exception ex){richTextBox1.Text += Environment.NewLine + ex.Message;}}
14.3 获得图片并显示
private void button3_Click(object sender, EventArgs e){try{NewsService.NewsInterfaceClient client = new NewsService.NewsInterfaceClient();byte[] buff=client.GetNewsImage("1");TypeConverter converter = TypeDescriptor.GetConverter(typeof(Bitmap));Bitmap bitmap = (Bitmap)converter.ConvertFrom(buff);pictureBox1.Image = bitmap;}catch (Exception ex){richTextBox1.Text += Environment.NewLine + ex.Message;}}
14.4 有的图片可能过大,可能响应的时候图片太大就不能显示了,我们需要修改宿主的WCF的配置
我们配置一下终结点(Endpoint)的绑定配置,名称就是 上面的<binding name=”这里的名字”>
我们还需要修改客户端的配置,使它可以接受超大的消息
打开NewsApplication的app.config文件
15.设置两个启动项目,运行查看效果
先生成解决方案,然后我们设置一下
效果图:
关于 获得列表,还有点问题
下次调,我再解决一下
关于NewsDto中我最后不是添加了一个NewsID吗,我没有添加DataMember属性,但是添加上去,似乎还是没有用,有人知道怎么解决吗
我把代码上传一下了,希望有人能棒棒我,感谢万分。 代码下载
这个是错误信息
接收对 http://localhost:1234/NewsMgr 的 HTTP 响应时发生错误。这可能是由于服务终结点绑定未使用 HTTP 协议造成的。这还可能是由于服务器中止了 HTTP 请求上下文(可能由于服务关闭)所致。有关详细信息,请参见服务器日志。
时间不早了,我先睡觉了,大家晚安
那天有个小孩教我WCF[一][2/3]的更多相关文章
- 那天有个小孩教我WCF[一][1/3]
那天有个小孩教我WCF[一][1/3] 既然是小孩系列,当然要有一点基础才能快速掌握,归纳,总结的一个系列,哈哈 前言: 第一篇嘛,不细讲,步步教你创建一个简单SOA案例,对WCF有个基本的认识,我不 ...
- (转)那天有个小孩教我WCF[一][1/3]
原文地址:http://www.cnblogs.com/AaronYang/p/2950931.html 既然是小孩系列,当然要有一点基础才能快速掌握,归纳,总结的一个系列,哈哈 前言: 第一篇嘛,不 ...
- WCF系列教程之WCF服务协定
本文参考自:http://www.cnblogs.com/wangweimutou/p/4422883.html,纯属读书笔记,加深记忆 一.服务协定简介: 1.WCF所有的服务协定层里面的服务接口, ...
- WCF系列教程之WCF服务宿主与WCF服务部署
本文参考自http://www.cnblogs.com/wangweimutou/p/4377062.html,纯属读书笔记,加深记忆. 一.简介 任何一个程序的运行都需要依赖一个确定的进程中,WCF ...
- 那天有个小孩跟我说LINQ(五)转载
2 LINQ TO SQL(代码下载) 我们以一个简单的销售的业务数据库为例子 表结构很简单:Users(购买者(用户)表),Products(产品信息表),Sales(销 ...
- WCF系列教程之WCF服务配置工具
本文参考自http://www.cnblogs.com/wangweimutou/p/4367905.html Visual studio 针对服务配置提供了一个可视化的配置界面(Microsoft ...
- WCF系列教程之WCF实例化
本文参考自http://www.cnblogs.com/wangweimutou/p/4517951.html,纯属读书笔记,加深记忆 一.理解WCF实例化机制 1.WCF实例化,是指对用户定义的服务 ...
- WCF系列教程之WCF中的会话
本文参考自http://www.cnblogs.com/wangweimutou/p/4516224.html,纯属读书笔记,加深记忆 一.WCF会话简介 1.在WCF应用程序中,回话将一组消息相互关 ...
- WCF系列教程之WCF客户端异常处理
本文参考自:http://www.cnblogs.com/wangweimutou/p/4414393.html,纯属读书笔记,加深记忆 一.简介 当我们打开WCF基础客户通道,无论是显示打开还是通过 ...
随机推荐
- Python 可命名元祖
import collections MytupleClass = collections.namedtuple('MytupleClass',['x','y','z']) obj = Mytuple ...
- Storm通信机制(了解)
Worker间的通信:经常需要通过网络跨节点进行,Storm使用ZeroMQ或Netty(0.9以后默认使用)作为进程间通信的消息框架. Worker进程内部通信:不同worker的thread通信使 ...
- 6-1 平衡的括号 uva673
简单栈题 #include<bits/stdc++.h> using namespace std; int main() { int cas;cin>>cas;getchar( ...
- 007 关于Spark下的第二种模式——standalone搭建
一:介绍 1.介绍standalone Standalone模式是Spark自身管理资源的一个模式,类似Yarn Yarn的结构: ResourceManager: 负责集群资源的管理 NodeMan ...
- 类的 __call__ 和__repr__ 方法
__call__: 让类实例可以被调用: __str__ , __repr__ : 两个都能是类实例名能被打印,区别在于repr可在交互是直接打印类名不用加print
- 10.16 NOIP模拟赛
目录 2018.10.16 NOIP模拟赛 A 购物shop B 期望exp(DP 期望 按位计算) C 魔法迷宫maze(状压 暴力) 考试代码 C 2018.10.16 NOIP模拟赛 时间:2h ...
- 51Nod.1237.最大公约数之和 V3(莫比乌斯反演 杜教筛 欧拉函数)
题目链接 \(Description\) \(n\leq 10^{10}\),求 \[\sum_{i=1}^n\sum_{j=1}^ngcd(i,j)\ mod\ (1e9+7)\] \(Soluti ...
- 数值分析之Neville's Algorithm
Neville插值方法详解 牛顿的插值方法涉及两个步骤:计算系数,随后评估多项式. 如果插值运作良好使用相同的多项式在x的不同值处重复执行. 要是一点是内插,一种单步计算插值的方法,如Nevi ...
- [AGC025B]RGB Coloring
[AGC025B]RGB Coloring 题目大意: 有\(n(n\le3\times10^5)\)个格子,每个格子可以选择涂成红色.蓝色.绿色或不涂色,三种颜色分别产生\(a,b,a+b(a,b\ ...
- python 条件语句和基础数据类型
条件语句 if 条件: pass else: pass 如果1等于1,输出欢迎进入东京热,否则输出欢迎进入一本道 ==: print("欢迎进入东京热") else: print( ...