WCF 流式处理 - 限制速度

本文关键字:速度 处理 WCF | 更新日期: 2023-09-27 18:35:30

这是我

几个月来的申请中的一个严重问题,但没有找到任何好的解决方案。我注意到 C# 管理 Stream 类在 WCF 中的流式处理方式,而不考虑我的配置。

首先,我有一个从 FileStream 继承的类,所以我可以随时查看到目前为止从客户端读取了多少内容:

public class FileStreamWatching : FileStream
    {
        /// <summary>        
        /// how much was read until now        
        /// </summary>        
        public long _ReadUntilNow { get; private set; }
        public FileStreamWatching(string Path, FileMode FileMode, FileAccess FileAccess)
            : base(Path, FileMode, FileAccess)
        {
            this._ReadUntilNow = 0;
        }
        public override int Read(byte[] array, int offset, int count)
        {
            int ReturnV = base.Read(array, offset, count);
            //int ReturnV = base.Read(array, offset, count);
            if (ReturnV > 0)
            {
                _ReadUntilNow += ReturnV;
                Console.WriteLine("Arr Lenght: " + array.Length);
                Console.WriteLine("Read: " + ReturnV);
                Console.WriteLine("****************************");
            }
            return ReturnV;
        }
    }

其次,以下是我读取包含该文件的客户端流的服务方法。我的主要问题是FileStreamWatching.Read不会在我每次从下面的方法召唤它时启动,而是FileStreamWatching.Read每调用X次启动一次。奇怪。

*稍后看输出

    public void Get_File_From_Client(Stream MyStream)
    {
        using (FileStream fs = new FileStream(@"C:'Upload'" + "Chat.rar", FileMode.Create))
        {
            byte[] buffer = new byte[1000];
            int bytes = 0;
            while ((bytes = MyStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                fs.Write(buffer, 0, bytes);
                fs.Flush();
            }
        }
    }

这是每次激活FileStreamWatching.Read时客户端的输出:(Remmber缓冲区长度只有1000!)

抵达: 256,阅读: 256


抵达:4096,阅读: 4096


抵达:65536,阅读: 65536


抵达:65536,阅读: 65536


抵达:65536,阅读: 65536


抵达:65536,阅读: 65536


....直到文件转换完成。

问题:

  1. 我带到读取方法的缓冲区长度不是 256/4096/65536。它是1000。
  2. 每次
  3. 从服务调用 FileStreamWatch 类时,读取 FileStreamWatch 类都不会启动。

我的目标:

  1. 控制每次读取时我从客户端获得的响应量。

  2. FileStreamWatching.Read 将在每次我从服务调用它时启动。

我的客户端配置:

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IJob" transferMode="Streamed"/>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8080/Request2" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IJob" contract="ServiceReference1.IJob"
                name="BasicHttpBinding_IJob" />
        </client>
    </system.serviceModel>
</configuration>

我的服务配置(这里没有配置文件):

        BasicHttpBinding BasicHttpBinding1 = new BasicHttpBinding();
        BasicHttpBinding1.TransferMode = TransferMode.Streamed;
        //
        BasicHttpBinding1.MaxReceivedMessageSize = int.MaxValue;
        BasicHttpBinding1.ReaderQuotas.MaxArrayLength = 1000;
        BasicHttpBinding1.ReaderQuotas.MaxBytesPerRead = 1000;
        BasicHttpBinding1.MaxBufferSize = 1000;
        //
        ServiceHost host = new ServiceHost(typeof(JobImplement), new Uri("http://localhost:8080"));
        //
        ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
        behavior.HttpGetEnabled = true;
        //
        host.Description.Behaviors.Add(behavior);
        ServiceThrottlingBehavior throttle = new ServiceThrottlingBehavior();
        throttle.MaxConcurrentCalls = 1;
        host.Description.Behaviors.Add(throttle);
        //
        //
        host.AddServiceEndpoint(typeof(IJob), BasicHttpBinding1, "Request2");
        host.Open();

WCF 流式处理 - 限制速度

回复

:为什么是256/4K/65535?

我在这里看到了两种可能性:

  • 基本FileStream正在执行自己的内部缓冲。 它可能在内部调用read(array,offset,length)来填充其内部缓冲区,然后传回您请求的部分。 内部调用最终是递归的,直到它读取了整个文件。 然后,您的覆盖将停止显示任何内容。
  • 还有其他stream.read()签名未显示为被覆盖。如果任何代码路径最终调用其他read方法之一,则计数将关闭。

re:MyStream 不会每次都重新开始

MyStream的论点是否被处理过? 还是将其重新用于新流? 代码仅在构造函数中"重新启动",因此请确保在更改传入流时释放并重新构造对象。

您可以通过在达到 EOF 时显示某些内容来测试递归 EOF 情况。

如果添加静态变量,则对应用程序调用MyStream.Read和方法进入/退出进行计数,则可以测试意外递归。 如果它们不匹配,则FileStream正在进行内部(意外递归)调用。

-耶西