转: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进行 ...
随机推荐
- Testlink1.9.5的安装配置
前两天搭建了Testlink环境,在这里整理记录下过程中遇到的问题以及搭建流程.Testlink版本:1.9.5操作系统:Windows7 32bit 步骤一:安装XAMPP 下载解压xampp压缩包 ...
- 20171129 ASP.NET中使用Skin文件
在Web.config里加入<pages styleSheetTheme="DefSkin"/>再在App_Themes文件夹里建个DefSkin文件夹把css和Ski ...
- entry.define编程思路
0.lua将文字传给场景脚本. 1.场景脚本将pattern.define文件中的PAT当作子弹(水泡弹,带颜色) 2.用户的问题作为客户端的请求,发送给服务器端 3.服务器端接受客户端的问题请求 4 ...
- Centos7安装Openvpn
前言 搭建openvpn主要参考这篇博客,原文传送: http://www.jianshu.com/p/4bbf946222d5 所以你会发现步骤基本一样. 安装openvpn A:安装EPEL仓库 ...
- Linux命令:tar命令批量解压方法总结
tar命令批量解压方法总结 (2010-05-24 17:48:46) 转载▼ 标签: tar 批量解压 杂谈 分类: linux学习 由于linux的tar命令不支持批量解压,所以很多网友编写了好多 ...
- get请求乱码解决
1.修改tomcat的配置文件 <ConnectorURIEncoding="utf-8" connectionTimeout="20000" port= ...
- ssh tunnel 三种模式
环境介绍: 主机 位置 公网 内网IP 角色 host-a 局域网1 否 192.168.0.1 ssh client host-b 局域网2.公网 是 192.168.1.1 ssh server ...
- git bash 报错bash: *: command not found
默认安装的git bash某些功能是没有的,比如zip,在git bash下执行zip和unzip命令时会报错命令找不到,但值得庆幸的是,我们可以安装我们需要的命令,以下以zip命令为例,步骤如下: ...
- malloc调用后经历了什么?
进程生成虚拟地址空间,有堆地址,由于是虚拟地址,所以没有做内存碎片化处理,只是在虚拟内存不够的时候调用brk,进行堆大小的调整,然后申请到虚拟内存是页,同MMU映射到物理地址,然后并不是每个页都预先加 ...
- 配置完centos 6以后,大概需要安装的软件(主要是yum)
根据实践,把我的经验说一下,以后我自己也可以按照这个快速安装软件. 1. 配置源.百度网盘的tools/download/linux已经放了几个挺重要的 东西了. yum -y install epe ...