WCF Streaming - Speed ​​Limit

This is a serious problem in my application for several months when you find some good solution. I noticed that C # controls the flow of the Stream stream in WCF, not considering my configuration.

Firstly, I have a class that inherits from FileStream, so I can see how much time was read from the client side at any time:

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 stream that contains the file. My main problem is that FileStreamWatching.Read does not start every time I call it from this method below, instead of FileStreamWatching.Read starts once for every X time I call it .. Strange.

* Look further and then

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

This is a client-side result for every moment FileStreamWatching.Read is activated: (Remmber buffer length is only 1000!)

Arr. Length: 256, Read: 256


Arr. Length: 4096, Read: 4096


Arr. Length: 65536, Read: 65536


Arr. Length: 65536, Read: 65536


Arr. Length: 65536, Read: 65536


Arr. Length: 65536, Read: 65536


.... Until the file transfer is complete.

Problems:

  • The length of the buffer that I led to the reading method is not 256/4096/65536. This is 1000.
  • Reading from the FileStreamWatching class does not start every time I call it from a service.

My goals:

  • Controlling how much I get from the client for each reading.

  • FileStreamWatching.Read will be triggered every time I call it from a service.

My client configuration:

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

The configuration of my service (there is no configuration file here):

  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(); 
+7
source share
1 answer

re: why 256 / 4K / 65535?

I see two possibilities here:

  • The FileStream base performs its own internal buffering. It can internally call read(array,offset,length) to fill its internal buffer, and then pass back the part you requested. Internal calls end up recursively until they read the entire file. Then your redefinition stops displaying anything.
  • There are other stream.read() signatures that you don't show as overridden. If any code path ends with a call to one of the other read methods, then your calculations will be disabled.

re: MyStream does not start every time

Is the argument MyStream ? or is it reused for a new thread? Your code only "restarts" in the constructor, so make sure that the object is deleted and rebuilt when incoming streams change.

You can check the recursive case of EOF by also displaying something when the EOF is reached.

You can test for unexpected recursion by adding static variables that count application calls to MyStream.Read and an input / output method. If they do not match, then FileStream makes internal (randomly recursive) calls.

-Jesse

+2
source

All Articles