转:WCF传送二进制流数据基本实现步骤详解
来自:http://developer.51cto.com/art/201002/185444.htm
WCF传送二进制流数据基本实现步骤详解
WCF传送二进制流数据的相关操作方法在实际应用中是一个比较基础的操作应用。我们在这里将会针对此做一个详细介绍。
我们知道,在实现WCF传送二进制流数据这一操作过程中,会有一些限制因素。我们在实际应用中要特别注意这一点。今天我们就会针对这方面的问题做一个详细的介绍,希望对大家有所帮助。
只有 BasicHttpBinding、WebHttpBinding、NetTcpBinding 和 NetNamedPipeBinding 支持传送流数据。
流数据类型必须是可序列化的 Stream 或 MemoryStream。
传递时消息体(Message Body)中不能包含其他数据。
我们先看看下面的WCF传送二进制流数据例子。
注意将 Binding.TransferMode 设置为 TransferMode.Streamed,我们还可以修改 Binding.MaxReceivedMessageSize 来调整消息大小(默认是64KB)。
[ServiceContract]
public interface IFileService
{
[OperationContract]
void Upload(Stream stream);
}
public class FileService : IFileService, IDisposable
{
public void Upload(Stream stream)
{
FileStream file = new FileStream("test.dll", FileMode.Create);
try
{
BinaryWriter writer = new BinaryWriter(file);
BinaryReader reader = new BinaryReader(stream);
byte[] buffer; do { buffer = reader.ReadBytes(); writer.Write(buffer); } while (buffer.Length > );
}
finally
{
file.Close();
stream.Close();
}
}
public void Dispose()
{
Console.WriteLine("Dispose...");
}
}
public class WcfTest
{
public static void Test()
{
AppDomain.CreateDomain("Server").DoCallBack(delegate
{
ServiceHost host = new ServiceHost(typeof(FileService), new Uri("http://localhost:8080/FileService"));
BasicHttpBinding binding = new BasicHttpBinding();
binding.TransferMode = TransferMode.Streamed;
host.AddServiceEndpoint(typeof(IFileService), binding, "");
host.Open();
});
BasicHttpBinding binding2 = new BasicHttpBinding();
binding2.TransferMode = TransferMode.Streamed;
IFileService channel = ChannelFactory<IFileService>.CreateChannel(binding2, new EndpointAddress("http://localhost:8080/FileService"));
using (channel as IDisposable)
{
FileStream stream = new FileStream("MyLibrary2.dll", FileMode.Open);
channel.Test(stream);
stream.Close();
}
}
}
一切正常。那么 "传递时消息体(Memory Body)中不能包含其他数据" 是什么意思?我们修改一下上面的契约,除了传递文件流外,我们还希望传递文件名。
[ServiceContract]
public interface IFileService
{
[OperationContract]
void Upload(string filename, Stream stream);
}
// ... 其他代码暂略 ...
当你修改完WCF传送二进制流数据的代码后,运行时你发现触发了一个 InvalidOperationException 异常。
未处理 System.InvalidOperationException
Message="For request in operation Upload to be a stream the operation must have a single parameter whose type is Stream."
Source="System.ServiceModel"
那么该怎么办呢?DataContract 肯定不行。 没错!你应该记得 MessageContract,将 filename 放到 MessageHeader 里面就行了。
[MessageContract]
public class FileData
{
[MessageHeader]
public string filename;
[MessageBodyMember]
public Stream data;
}
[ServiceContract]
public interface IFileService
{
[OperationContract]
void Upload(FileData file);
}
public class FileService : IFileService, IDisposable
{
public void Upload(FileData file)
{
FileStream f = new FileStream(file.filename, FileMode.Create);
try
{
BinaryWriter writer = new BinaryWriter(f);
BinaryReader reader = new BinaryReader(file.data);
byte[] buffer;
do
{
buffer = reader.ReadBytes(); writer.Write(buffer);
} while (buffer.Length > );
}
finally
{
f.Close(); file.data.Close();
}
}
public void Dispose()
{
Console.WriteLine("Dispose...");
}
}
public class WcfTest
{
public static void Test()
{
AppDomain.CreateDomain("Server").DoCallBack(delegate
{
ServiceHost host = new ServiceHost(typeof(FileService), new Uri("http://localhost:8080/FileService"));
BasicHttpBinding binding = new BasicHttpBinding();
binding.TransferMode = TransferMode.Streamed;
host.AddServiceEndpoint(typeof(IFileService), binding, "");
host.Open();
});
BasicHttpBinding binding2 = new BasicHttpBinding();
binding2.TransferMode = TransferMode.Streamed;
IFileService channel = ChannelFactory<IFileService>.CreateChannel(binding2, new EndpointAddress("http://localhost:8080/FileService"));
using (channel as IDisposable)
{
FileData file = new FileData();
file.filename = "test2.dll";
file.data = new FileStream("MyLibrary2.dll", FileMode.Open);
channel.Upload(file); file.data.Close();
}
}
}
问题解决了。上面的例子使用 BaseHttpBinding,如果使用 NetTcpBinding,相信速度要快很多。除了向服务器传送流外,也可反向返回流数据。
[ServiceContract]
public interface IFileService
{
[OperationContract]
void Upload(Stream stream);
[OperationContract]
Stream Download(string filename);
}
虽然服务器在操作结束时会自动关闭客户端 Request Stream,但个人建议还是使用 try...finnaly... 自主关闭要好一些,因为意外总是会发生的。
WCF传送二进制流数据的全部操作方法就为大家介绍到这里。
转:WCF传送二进制流数据基本实现步骤详解的更多相关文章
- Jmeter入门13 jmeter发送application/octet-stream二进制流数据
http接口请求header里面 content-type: application/octet-stream (二进制流数据),如何用jmeter发送请求? 1 添加http请求头 2 http请 ...
- [转帖]IP /TCP协议及握手过程和数据包格式中级详解
IP /TCP协议及握手过程和数据包格式中级详解 https://www.toutiao.com/a6665292902458982926/ 写的挺好的 其实 一直没闹明白 网络好 广播地址 还有 网 ...
- Oracle10g数据泵impdp参数详解--摘自网络
Oracle10g数据泵impdp参数详解 2011-6-30 12:29:05 导入命令Impdp • ATTACH 连接到现有作业, 例如 ATTACH [=作业名]. • C ...
- Code First开发系列之管理数据库创建,填充种子数据以及LINQ操作详解
返回<8天掌握EF的Code First开发>总目录 本篇目录 管理数据库创建 管理数据库连接 管理数据库初始化 填充种子数据 LINQ to Entities详解 什么是LINQ to ...
- 8天掌握EF的Code First开发系列之3 管理数据库创建,填充种子数据以及LINQ操作详解
本文出自8天掌握EF的Code First开发系列,经过自己的实践整理出来. 本篇目录 管理数据库创建 管理数据库连接 管理数据库初始化 填充种子数据 LINQ to Entities详解 什么是LI ...
- <转>ASP.NET学习笔记之MVC 3 数据验证 Model Validation 详解
MVC 3 数据验证 Model Validation 详解 再附加一些比较好的验证详解:(以下均为引用) 1.asp.net mvc3 的数据验证(一) - zhangkai2237 - 博客园 ...
- python数据分析数据标准化及离散化详解
python数据分析数据标准化及离散化详解 本文为大家分享了python数据分析数据标准化及离散化的具体内容,供大家参考,具体内容如下 标准化 1.离差标准化 是对原始数据的线性变换,使结果映射到[0 ...
- ajax 请求二进制流 图片 文件 XMLHttpRequest 请求并处理二进制流数据 之最佳实践
写在前面 :从提出需求到完美的解决问题,实现过程是曲折的. 需求:在前(web client)后(Restful Service)端完全解耦的模式框架下,webclient需要请求 Service 返 ...
- socket传送二进制流的一些总结
第一次实质性的接触socket通信方面的工作,所以遇到的问题还真不少,写篇博客记录一下,提升下记忆. 需求是通过私有协议进行二进制数据的传输,必须保证数据包不能被丢失,所以选择tcp的socket进行 ...
随机推荐
- private static final Logger logger= LoggerFactory.getLogger(WhMainBusi.class);
LoggerFactory.getLogger(WhMainBusi.class):指定类初始化日志对象,在日志输出的时候,将会打印日志信息所在的类.如: logger.info("日志信息 ...
- 20171228 C#值类型和引用类型
public class RefPoint //定义的引用类型 { public int x; public RefPoint(int x) { this.x = x; } } public stru ...
- 火币网API文档——Websocket 请求与订阅示例
1. 访问地址 Pro 站行情请求地址为:wss://api.huobipro.com/ws HADAX 站行情请求地址为:wss://api.hadax.com/ws 2. 数据压缩 WebSock ...
- Android使用SpannableString设置多样式文本
Android将一行文本设置为多种样式时,可以使用 SpannableString 来实现 private void setTips(){ String big = "大字深色"; ...
- openvpn-客户端配置文件
############################################## # 针对多个客户端的OpenVPN 2.0 的客户端配置文件示例 # # 该配置文件可以被多个客户端使用, ...
- Apache服务安全加固
一.账号设置 以专门的用户帐号和用户组运行 Apache 服务. 根据需要,为 Apache 服务创建用户及用户组.如果没有设置用户和组,则新建用户,并在 Apache 配置文件中进行指定. 创建 A ...
- [Java in NetBeans] Lesson 13. Multidimensional Arrays
这个课程的参考视频和图片来自youtube. 主要学到的知识点有: 1. Multidimensional Array: Array that has more than one dimension. ...
- CentOS6.5安装Elasticsearch1.7.5
1. 首页到官方网站下载最新安装包 https://www.elastic.co/downloads/elasticsearch elasticsearch-1.7.5.tar.gz 2. 将软件包 ...
- 时间序列模式(ARIMA)---Python实现
时间序列分析的主要目的是根据已有的历史数据对未来进行预测.如餐饮销售预测可以看做是基于时间序列的短期数据预测, 预测的对象时具体菜品的销售量. 1.时间序列算法: 常见的时间序列模型; 2.时序模 ...
- histogram 和 bar plot的区别
最本质的区别是这样的:histogram用来描述的是numerical变量,而bar plot用来描述的是categorical类型的变量.统计学当中关于变量的分类 这可以从它们的图形上面看到: hi ...