1.创建service和client项目

service项目新建wcf服务文件 MediaService 和 IMediaService

IMediaService 代码为

 using System.IO;
using System.ServiceModel; namespace Wcf_Stream_Service
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IMediaService”。
[ServiceContract]
public interface IMediaService
{
[OperationContract]
string[] GetMediaList(); [OperationContract]
Stream GetMedia(string mediaName);
}
}

MediaService 代码为

 using System;
using System.IO; namespace Wcf_Stream_Service
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“MediaService”。
public class MediaService : IMediaService
{
public Stream GetMedia(string mediaName)
{
string filePath = @"F:\Projects\Learn4Soft\Wcf_Stream_Service\bin\Debug\" + mediaName + ".txt";
if (!File.Exists(filePath))
{
throw new Exception("不存在文件");
}
Stream s = null;
try
{
s = new FileStream(filePath, FileMode.Open, FileAccess.Read);
}
catch (Exception)
{
//如果发生异常了 我们就将流关闭释放
s.Close();
}
return s;
} public string[] GetMediaList()
{
string[] mediaList = new string[];
mediaList[] = "lemon tree";
mediaList[] = "yesterday";
mediaList[] = "500 miles";
return mediaList;
}
}
}

service服务对应的app.Config内容为:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<!--bindings节点为自己新增的,旨在添加两个Streamed的传输,另外设置了最长信息接收量 最后为binding定义了一个名字-->
<bindings>
<basicHttpBinding>
<binding name="newBasicHttpBinding" transferMode="Streamed" maxReceivedMessageSize="200000" />
</basicHttpBinding> <netTcpBinding>
<binding name="newNetTcpBinding" transferMode="Streamed" maxReceivedMessageSize="200000">
<!--设置无安全-->
<security mode ="None" />
</binding>
</netTcpBinding>
</bindings> <services>
<service name="Wcf_Stream_Service.MediaService">
<!--端点取了名字 以及制定了bindingConfiguration-->
<endpoint address=""
binding="basicHttpBinding"
bindingConfiguration="newBasicHttpBinding"
name="basicHttpBinding_IMetadataExchange"
contract="Wcf_Stream_Service.IMediaService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address=""
binding="netTcpBinding"
bindingConfiguration="newNetTcpBinding"
name="NetTcpBinding_IMetadataExchange"
contract="Wcf_Stream_Service.IMediaService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/MediaService" />
<!--为tcp协议定义一个地址-->
<add baseAddress="net.tcp://localhost:8734/tcp" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>

最后是service类的main方法:

 using System;
using System.ServiceModel; namespace Wcf_Stream_Service
{
class Program
{
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(MediaService));
host.Open();
Console.WriteLine("服务已经启动");
Console.ReadKey();
}
}
}

2 接下来是client

首先以管理员身份启动service,然后引用这个服务引用自动在配置文件中生成了对应的wcf服务配置

最后直接就是main方法

 using System;
using System.IO; namespace Wcf_Stream_Client
{
class Program
{
static void Main(string[] args)
{
ServiceReference_Stream.MediaServiceClient client = new ServiceReference_Stream.MediaServiceClient("NetTcpBinding_IMetadataExchange");
string mediaName=client.GetMediaList()[];
Console.WriteLine(mediaName); Stream s = client.GetMedia(mediaName);
//var num = s.Length;
byte[] bytes = new byte[];
s.Read(bytes, , bytes.Length);
//s.Seek(0, SeekOrigin.Begin); FileStream fs = new FileStream("new_" + mediaName + ".txt", FileMode.Create);
BinaryWriter writer = new BinaryWriter(fs);
writer.Write(bytes);
writer.Close();
fs.Close();
Console.ReadKey();
}
}
}

特别要备注的一点是:流传输并不是一定非要让接口请求的方法的返回值的Stream类型,什么类型都可以,只是他们在传输过程中会变成流,接收后按照协议栈又会从流转成方法返回值对应的类型。如果不按流传输,按buffer传输,我们也没有将返回值转成byte不是吗!

6 Wcf使用Stream传输的更多相关文章

  1. WCF利用Stream上传大文件

    WCF利用Stream上传大文件 转自别人的文章,学习这个例子,基本上wcf也算入门了,接口用法.系统配置都有了 本文展示了在asp.net中利用wcf的stream方式传输大文件,解决了大文件上传问 ...

  2. WCF分布式开发步步为赢(4):WCF服务可靠性传输配置与编程开发

    今天继续WCF分布式开发步步为赢系列的第4节:WCF服务可靠性传输配置与编程开发.这个章节,我们要介绍什么是WCF服务的可靠性传输,随便介绍网络协议的概念,Web Service为什么不支持可靠性传出 ...

  3. WCF 笔记 (2) - 传输泛型 List 对象

    WCF 笔记 (2) - 传输泛型 List 对象 本帖介绍怎么在 WCF 中,在 Server-side 和 Client-side 之间,传递默认无法传输的 List<T>.List& ...

  4. C# WCF学习笔记(二)终结点地址与WCF寻址(Endpoint Address and WCF Addressing) WCF中的传输协议

    URI的全称是 Uniform Rosource Identifire(统一资源标识),它唯一标识一个确定的网绐资源,同时也表示资源所处的位置及访问的方式(资源访问所用的网络协议). 对于Endpoi ...

  5. 使用Fiddler解析WCF RIA Service传输的数据

    原文 http://www.cnblogs.com/wintersun/archive/2011/01/05/1926386.html 使用Fiddler 2 解析WCF RIA Service传输的 ...

  6. WCF基础之传输

    WCF中使用的主要传输的方式有HTTP,TCP和命名管道. 绑定包括可选的协议绑定元素(如安全),必需的编码绑定元素和必须的传输协定绑定元素三个部分,而由传输方式则是由传输绑定元素来决定的. HTTP ...

  7. WCF大文件传输【转】

    http://www.cnblogs.com/happygx/archive/2013/10/29/3393973.html WCF大文件传输 WCF传输文件的时候可以设置每次文件的传输大小,如果是小 ...

  8. WCF大文件传输服务

    由于项目需要,自己写一个基于WCF的大文件传输服务雏形.觉得有一定的参考价值,因此放在网上分享. 目前版本为v1.1特点如下: 1.文件传输端口为18650 2.上传和下载文件 3.支持获取文件传输状 ...

  9. 转:wcf大文件传输解决之道(2)

    此篇文章主要是基于http协议应用于大文件传输中的应用,现在我们先解析下wcf中编码器的定义,编码器实现了类的编码,并负责将Message内存中消息转变为网络发送的字节流或者字节缓冲区(对于发送方而言 ...

随机推荐

  1. storm-kafka编程指南

    目录 storm-kafka编程指南 一.原理及关键步骤介绍 (一)使用storm-kafka的关键步骤 1.创建ZkHosts 2.创建KafkaConfig 3.设置MultiScheme 4.创 ...

  2. css3-12 transform:scale(1.2,1.2)实现移入元素变大特效

    css3-12 transform:scale(1.2,1.2)实现移入元素变大特效 一.总结 一句话总结:transform:scale(1.2,1.2)鼠标移入的时候变大一点点,超出边框的部分隐藏 ...

  3. IPv4与IPv6数据报格式详解

    摘要: 本文给出IPv4与IPv6数据报格式示意图,并整理了各个字段含义,最后对比IPv4与IPv6数据报格式的区别. 一.IPv4数据报 图1 IPv4数据报格式版本号(version) 不同的IP ...

  4. Internet连接共享只能上qq不能打开网页的问题解决

    作者:朱金灿 来源:http://blog.csdn.net/clever101 之前我写过一篇<Windows共享上网的做法>,在设置共享网络时是有一个家庭网络连接的选项的,如下图: 但 ...

  5. 【u230】回文词

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] CR喜欢研究回文词,有天他发现一篇文章,里面有很多回文数,这使他来了兴趣.他决定找出所有长度在n个字节 ...

  6. Mongodb in Mycat指南

    1       前言 Mycat目前支持JDBC连接后端数据库,理论上支持任何数据库,如ORACLE.DB2.SQL Server等,是将其模拟为MySQL,所以对其他数据库只支持标准的SQL语句,而 ...

  7. 【TP SRM 703 div2 500】 GCDGraph

    Problem Statement You are given four ints: n, k, x, and y. The ints n and k describe a simple undire ...

  8. HTML5开发移动web应用——SAP UI5篇(9)

    之前我们对于app的构建都是基于显示的.如今我们来格式化一下,引入很多其它的SAP UI5组件概念.这使得APP的一个界面更有层次性.更像是一个手机应用的界面,而且更好地使用SAP UI5中提供的功能 ...

  9. php实现包含min函数的栈(这个题目用另外一个栈做单调栈的话时间复杂度会低很多)

    php实现包含min函数的栈(这个题目用另外一个栈做单调栈的话时间复杂度会低很多) 一.总结 这个题目用另外一个栈做单调栈的话时间复杂度会低很多 二.php实现包含min函数的栈 题目描述 定义栈的数 ...

  10. GLPI-开源资产管理软件

    https://github.com/glpi-project/glpi/releases/tag/9.2.3 http://glpi-project.org/downloads/ 开源资产管理软件- ...