这是一个严重的问题,在我的应用程序几个月没有找到任何好的解决方案。 我注意到C#管理Stream类在WCF中流式传输的方式,而不考虑我的配置。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缓冲区lenght仅1000!)
编曲长度:256, 阅读:256
编曲长度:4096, 阅读:4096
编曲长度:65536, 阅读:65536
编曲长度:65536, 阅读:65536
编曲长度:65536, 阅读:65536
编曲长度:65536, 阅读:65536
....直到文件transfare完成。
问题:
- 我带到read方法心不是256/4096/65536缓冲区的Lenght。它是1000.
- 从服务中调用FileStreamWatching类的Read不会启动。
我的目标:
Controling上我从客户的每次读多少reacive。
每次我从服务调用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();
MyStream.Read(缓冲,0,buffer.Length) - 不应该每次之后的偏移量增量打电话? – Tisho
@Tisho应用程序可能会改变每次调用的偏移量。代码中的这一点是否影响斯塔夫的问题。 –