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. [Err] 1136 - Column count doesn&#39;t match value count at row 1

    1 错误描写叙述 [Err] 1136 - Column count doesn't match value count at row 1 Procedure execution failed 113 ...

  2. 23种设计模式——Prototype模式

    Prototype模式是提供自我复制的功能.包括浅拷贝和深拷贝. 一.Prototype模式的用途 场景1:游戏场景中有很多类似的敌人,它们的技能都一样,但是随着敌人出现的位置和不同,它们的能力也不太 ...

  3. 使用Perl脚本编译Latex

    使用Perl脚本编译Latex 脚本能实现Latex文本的初级编译,并将生成的中间文件移动到同一个目录 调用方法 chmod +x xelatex2pdf.pl xelatex2pdf.pl -n 2 ...

  4. [Angular] Creating an Observable Store with Rx

    The API for the store is really simple: /* set(name: string, state: any); select<T>(name: stri ...

  5. Android基础新手教程——1.2 开发环境搭建

    Android基础新手教程--1.2 开发环境搭建 标签: Android基础新手教程 如今主流的Android开发环境有: ①Eclipse + ADT + SDK ②Android Studio ...

  6. 2016 Java程序员的年终总结(转)

    2016 Java程序员的年终总结 技术积累 (1)代码规范 1.1.1.通常的模块分布:一般如果你要实现一个web 应用,你从后台将数据展示到前端页面,在一个比较大的公司,你少不了跟其他项目有交集( ...

  7. linux上电自启动应用程序具体解释

    每当我学习一个新的东西得时候都是会 遇到一些错误.可是我会很努力的去解决它,今天这个自启动应用程序花了我两个小时的时间才攻克了.所以说遇到问题的时候要去思考.分析.以下我就来谈谈linux上电自启动应 ...

  8. 三列自适应布局的实现方式(兼容IE6+)

    1.绝对定位方式 <div class="nm-3-lr"> <div class="aside-f"> <p>侧边栏1固定 ...

  9. ios开发总结:Utils常用方法等收集,添加扩展类,工具类方法,拥有很多方便快捷功能(不断更新中。。。)

    BOBUtils 工具大全 本人github开源和收集功能地址:https://github.com/niexiaobo [对ios新手或者工作一年以内开发人员很有用处] 常用方法等收集.添加扩展类. ...

  10. 【codeforces 755B】PolandBall and Game

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...