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不是吗!

最新文章

  1. XcodeiOS模拟器安装相关
  2. FusionChart 数据的传入方式
  3. em
  4. Ueditor配置及在项目中的使用
  5. [LA3026]Period
  6. SU sugain命令学习
  7. myeclipse显示行号
  8. 启动httpd服务:SSLCertificateFile: file &#39;/var/www/miq/vmdb/certs/server.cer&#39; does not exist or is empty
  9. 68.vivado与modelsim的关联以及器件库编译
  10. jumplist和changlist
  11. java中volatitle关键字的作用
  12. UILabel,文字添加下划线,中划线
  13. 6 个轻量级并且灵活的 PHP CMS 系统
  14. Dom4j 添加 / 更新 / 删除 XML
  15. 发布支持多线程的PowerShell模块 &mdash;&mdash; MultiThreadTaskRunner
  16. 零基础学Python--------第11章 使用Python操作数据库
  17. P3420 [POI2005]SKA-Piggy Banks
  18. com.fasterxml.jackson.databind.JsonMappingException
  19. 浅析STM32之usbh_def.H
  20. GUN WINDOW 工具

热门文章

  1. 基于mpvue的小程序项目搭建的步骤一
  2. 算法 Tricks(四)—— 判断序列中的字符/数值是否交替出现
  3. [原]MFC中DIALOG(对话框)程序响应加速键(快捷键)
  4. ios 获取button所在的cell对象, 注意:ios7 =&amp;lt; System Version &amp;lt; ios8 获取cell对象的差别
  5. 《编程导论(Java)&amp;#183;4.1数据抽象的含义》
  6. css3-11 如何改变背景图片的大小和位置
  7. 安装使用jupyter(原来的notebook)
  8. 一个神奇的控件——Android CoordinatorLayout与Behavior使用指南
  9. 利用函数的惰性载入提高 javascript 代码性能
  10. 网络拓扑图js插件——jTopo应用