Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法理解为什么FileStream会将文件加载到内存中_C#_Wcf_Filestream - Fatal编程技术网

C# 无法理解为什么FileStream会将文件加载到内存中

C# 无法理解为什么FileStream会将文件加载到内存中,c#,wcf,filestream,C#,Wcf,Filestream,我在WCF中开发,在我的客户机代码中,我创建了文件流,其中包含一个要传输到服务的文件(通过在消息中以流的形式发送)。服务人员收到信息流后,他一个接一个地读,然后写在新文件上;服务人员将文件从客户端下载给他,仅此而已 我重写了filestream中的Read方法,因此我可以知道在客户端下载服务时读取了多少内容 public class FileStreamEx : FileStream { public long _ReadUntilNow { get; private s

我在WCF中开发,在我的客户机代码中,我创建了文件流,其中包含一个要传输到服务的文件(通过在消息中以流的形式发送)。服务人员收到信息流后,他一个接一个地读,然后写在新文件上;服务人员将文件从客户端下载给他,仅此而已

我重写了filestream中的Read方法,因此我可以知道在客户端下载服务时读取了多少内容

public class FileStreamEx : FileStream
    {
        public long _ReadUntilNow { get; private set; }
        public FileStreamEx(string path, FileMode mode, FileAccess access, FileShare share)
            : base(path, mode, access, share)
        {
            this._ReadUntilNow = 0;
        }
        public override int Read(byte[] array, int offset, int count)
        {
            int ReturnV = base.Read(array, offset, count);
            _ReadUntilNow += ReturnV;
            return ReturnV;
        }
    }
我的目标是从客户端了解服务每秒读取的数据量。 这可以通过仅从客户端查看_ReadUntilNow值来实现,因为服务仅使用对FileStreamEx对象的引用

我的问题是,在服务开始读取我给他的流之前,_readuntilnowvalue=文件的大小。要将其转换为hepend,唯一的方法是在我执行之前调用我的重写方法Read

我的问题是谁先给我打电话,为什么以及我能做些什么来防止它?

我的客户:

public static void CallService()
        {
            ServiceReference1.IJob Service1 = new ServiceReference1.JobClient(new InstanceContext(new CCBImplement()));
            DLL.FileStreamEx TheStream TheStream = new DLL.FileStreamEx(@"C:\Uploadfiles\Chat.rar", FileMode.Open, FileAccess.Read, FileShare.None);
            ServiceReference1.RemoteFile RemoteFile1=new ServiceReference1.RemoteFile("Chat.rar", TheStream.Length, @"C:\Uploadfiles\Chat.rar", TheStream);
            Service1.UselessMethod1(RemoteFile1);
            new Thread(new ThreadStart(Check_Only_ReadUntilNow_Every_Second)).Start();
        }
服务代码:

public void UselessMethod1(RemoteFile RemoteFile)
        {
            Stream MyStream = RemoteFile.FileByteStream;
            using (FileStream fs = new FileStream(@"C:\Upload\"+RemoteFile.FileName, FileMode.Create))
            {
                int bufferSize = 1 ; // 1 MB buffer
                byte[] buffer = new byte[bufferSize];
                int totalBytes = 0;
                int bytes = 0;
                while ((bytes = MyStream.Read(buffer, 0, bufferSize)) > 0)
                {
                    fs.Write(buffer, 0, bytes);
                    fs.Flush();
                    totalBytes += bytes;
                }
            }
        }
远程文件类:

[MessageContract]
    public class RemoteFile : IDisposable
    {
        [MessageHeader]
        public string FileName;

        [MessageHeader]
        public string Path;

        [MessageHeader]
        public long Length;

        [MessageBodyMember]
        public Stream FileByteStream;
        public RemoteFile() { }
        public RemoteFile(string FileName, string Path, long Length, Stream FileByteStream)
        {
            this.Path = Path;
            this.FileName = FileName;
            this.Length = Length;
            this.FileByteStream = FileByteStream;
        }
        public void Dispose()
        {
            if (FileByteStream != null)
            {
                FileByteStream.Close();
                FileByteStream = null;
            }
        }
    }

我认为他的问题是: 在A端,有一个服务主机,其方法是以流模式读取文件。 在B端,有一个客户端使用此服务发送文件

他扩展了FileStream CALS以添加一个变量,该变量保存从文件读取的字节数

他在A端调用服务,但在它到达方法之前-额外变量保存整个文件的大小..这意味着整个文件已被读取


问题是为什么。

嗯。。。什么?听起来你想知道服务调用另一端内部发生的信息……我只传递流对象的引用,这样我就可以知道了。你能更清楚一点吗?谁是“某人”,他们如何称呼Read?另外,向我们展示您的WCF配置。它甚至被配置为流式传输数据吗?如果我们根本不知道MyStreamEx是如何被调用的,我们就不能告诉你为什么它会在其他东西之前被调用。对,那么基本上是:“为什么数据没有流式传输?”没错。他的配置正常…将传输模式声明为流。