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. hdu 5375 Gray code dp

    #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; cons ...

  2. iOS ASIHTTPRequest

    ASIHTTPRequest对CFNetwork API进行了封装,并且使用起来非常简单,用Objective-C编写,可以很好的应用在Mac OS X系统和iOS平台的应用程序中.ASIHTTPRe ...

  3. 关于Android中设置闹钟的相对比较完善的解决方案

    我当时说承诺为大家写一个,一直没空,直到最近又有人跟我要,我决定抽时间写一个吧.确实设置闹钟是一个比较麻烦的东西.我在这里写的这个demo抽出来了封装了一个类库,大家直接调用其中的设置闹钟和取消闹钟的 ...

  4. css盒子模型的宽度问题

    最近看css权威指南的时候,发现一个之前特别不清楚的概念——宽度. 每个块级元素都有一个元素框,元素框内包括了元素内容,元素内边距,元素边框,元素外边距. 所以元素框的宽度=元素内容宽度+元素内边距+ ...

  5. MySQL启动关闭添加到 /etc/init.d/mysqld

      cp /data/mysql/support-files/mysql.server /etc/init.d/mysqld       然后就可以使用此命令启动/关闭 mysql: /etc/ini ...

  6. 反向代理:是指以代理server来接收Internet上的请求,然后将请求转发到内部网络的server上,并将结果返回给Internet上连接的client,此时的代理server对外就表现为反向代理server。

       Nginx安装好之后.開始使用它来简单实现反向代理与负载均衡的功能.在这之前.首先得脑补一下什么是反向代理和负载均衡.   反向代理:是指以代理server来接收Internet上的请求,然后将 ...

  7. iOS开发Quartz2D 三 进度条的应用

    一:效果如图: 二:代码 #import "ViewController.h" #import "ProgressView.h" @interface View ...

  8. C#生成6位随机验证码

    private string VerifyCode() { Random random = new Random(); , ).ToString(); }

  9. bash 提示用户输入 choice

    read -p "Continue (y/n)?" choice case "$choice" in y|Y ) echo "yes";; ...

  10. Net Reactor 5

    Net Reactor 5脱壳教程   今天别人发来一个.Net的DLL让我脱壳,第一步自然是先扔进de4dot 我这个de4dot 是集成了  Ivancito0z / TheProxy / PC- ...