惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
爱范儿
爱范儿
H
Help Net Security
V
Visual Studio Blog
J
Java Code Geeks
Stack Overflow Blog
Stack Overflow Blog
Microsoft Security Blog
Microsoft Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
MyScale Blog
MyScale Blog
The Cloudflare Blog
Martin Fowler
Martin Fowler
D
Docker
腾讯CDC
F
Fortinet All Blogs
雷峰网
雷峰网
GbyAI
GbyAI
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Announcements
Recent Announcements
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Blog — PlanetScale
Blog — PlanetScale
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
博客园 - 叶小钗

博客园 - JerryShi

对象序列化之 替换(Surrogate) 使用 IExtensibleDataObject 进行往返式序列化 使用 NetDataContractSerializer 共享类型 WCF可序列化方式 序列化 VS 编码 自定义 Behavior 操作行为 之 事务 服务行为 之 元数据 服务行为 之 并发与实例化 基本的WCF编程(实例) WCF Behaviors(行为) WCF 体系结构 WCF服务 使用netTcpBinding 无法打开默认数据库 SharePoint 创建SSP时出现找不到 Windows NT 用户或组 的异常 SQL Server 2008 Mirror 泛型(一) 自定义属性Attribute(三) 自定义属性Attribute(二)
使用流模式传输大型数据
JerryShi · 2011-12-14 · via 博客园 - JerryShi

WCF 支持两种消息处理模式:

缓冲模式:WCF处理消息的默认方式,在缓冲模式下,整个消息都会保存在内存中,知道发送或者接收完成。此模式作为默认方式,在某些情况下是必需的,如可靠消息传送和数字签名,但存在的缺陷是如果消息数据过大,缓冲占用大量系统内存,同时消耗其他系统资源。

流模式:使用System.IO.Stream 来发传送消息,流模式通常在绑定或传输信道上打开,在绑定的配置上设置transferMode属性来控制流模式的粒度。

transferMode的属性值包括:Buffer、Streamed、StreamedResponse、StreamedRequest;

Demo

Contract:

    [ServiceContract]
    public interface IFileDownload
    {
        [OperationContract]
        Stream GetFileStream(string fileName);
    }

Service:

    public class FileDownloadService : IFileDownload, IDisposable
    {
        private FileStream fStream = null;

        public Stream GetFileStream(string fileName)
        {
            string filePath = AppDomain.CurrentDomain.BaseDirectory + fileName;
            if (!File.Exists(filePath))
            {
                throw new ArgumentException(string.Format("无法找到文件名为{0}的文件", fileName));
            }

            fStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            return fStream;
        }

        public void Dispose()
        {
            Dispose(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            //被客户直接调用的,那么托管的,和非托管的资源都可以释放  if (disposing)
            {
                // 释放 托管资源  fStream.Dispose();
                //垃圾回收器从Finalization队列中清除自己,从而阻止垃圾回收器调用Finalize方法.          GC.SuppressFinalize(this);
            }
        }
    }

Config

<service name="JerryShi.EssentialWCF.FileDownloadService"  behaviorConfiguration="StockServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:809/FileDownloadService"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="netTcpBinding"  contract="JerryShi.EssentialWCF.IFileDownload" bindingConfiguration="EnableStreamingOnNetTcp" />
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <bindings>
      <netTcpBinding>
        <binding name="EnableStreamingOnNetTcp" transferMode="Streamed" />
      </netTcpBinding>
    </bindings>

说明

运行过程中出现一次异常,异常如下:

error

此异常由于Client端配置与Server端配置不匹配导致的

<binding name="NetTcpBinding_IFileDownload" closeTimeout="00:01:00"
                   openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                   transactionFlow="false" transferMode="Streamed" transactionProtocol="OleTransactions"
                   hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                   maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
                   maxReceivedMessageSize="65536">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00"
              enabled="false" />
          <security mode="Transport">
            <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
            <message clientCredentialType="Windows" />
          </security>
        </binding>