English 中文(简体)
WCF WCF 流流 - 限制速度
原标题:WCF Streaming - Limiting speed

This is a severe problem in my application for some months with out finding any good solution. I noticed that C# manage the way Stream class is streaming in WCF, without considering my configuration.

首先,我有一个从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;
        }
    }

Secondly, below is my service method of reading the client s stream that contain the file. My main problem is that FileStreamWatching.Read does not start for each time I summon it from this method below, instead FileStreamWatching.Read start one time for each X times I call it.. Strange.

+++%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%+%++%++%++%+%+%+%+%+%+%+%++++++%++%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    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();
            }
        }
    }

< 坚固> 这是每次 FileStream Watching 时客户端的输出。 读被激活 : (remmber the Suide lenght is only 1000!)

Arr Lenght: 256, Read: 256


Arr Lenght: 4096, Read: 4096


Arr Lenght: 65536, Read: 65536


Arr Lenght: 65536, Read: 65536


Arr Lenght: 65536, Read: 65536


Arr Lenght: 65536, Read: 65536


...直到文件传送完成为止

<强 > 问题:

  1. The Lenght of the buffer I brought to the read method isnt 256/4096/65536. It is 1000.
  2. The Read from the FileStreamWatching class does not start each time I call it from the service.

我的目标:

  1. 控制我从客户那里 了解到每个读者的读数

  2. 我每次在服务处打电话时 都会开始阅读

<强> 我的客户端配置:

<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>

<% 1> 我的服务配置 (这里没有配置文件) :

        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();
最佳回答

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

我看到两种可能性:

  • The base FileStream is doing its own internal buffering. It may be internally calling read(array,offset,length) to fill its internal buffer, then passing back the portion you asked for. The internal calls end up being recursive, until it has read the entire file. Then your override stops displaying anything.
  • There are other stream.read() signatures you don t show as being overridden. If any code path ends up calling one of the other read methods, then your counts will be off.

< 坚固> : MyStream 不是每次开始

MyStream 参数曾经被处理过吗? 还是被重新用于新流? 您的代码只在构建器中“ 重新启动 ”, 所以在您更改流入的流时要 < em> 保证 对象被处理和重新构建 。

您可以在 EOF 到达 EOF 时,通过显示某个东西来测试 EOF 的递归大小写 。

如果您添加静态变量来计算 MyStream.Read 应用程序的调用以及方法条目/退出, 您可以测试意外循环。 如果它们不匹配, 那么 FileStream 正在进行内部( 偶然循环) 调用 。

-杰西

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签