简体   繁体   English

MemoryStream VS FileStream在Webservice中使用哪个更好地进行GZipStream解压缩?

[英]MemoryStream VS FileStream which one is better to use in Webservice for GZipStream decompress?

I have a asp.net webservice. 我有一个asp.net网络服务。 some functionality of this webservice is to decompress clients request first. 该Web服务的某些功能是首先解压缩客户端请求。 for this i have wrote 2 methods one uses MemoryStream and other uses FileStream . 为此,我编写了2种方法,一种使用MemoryStream ,另一种使用FileStream

When using MemoryStream sometiomes it get OutofMemoryException . 当使用MemoryStream某些对象时,它将获得OutofMemoryException so i have planned to use FileStream instead of MemoryStream for this reason. 因此,出于这个原因,我计划使用FileStream代替MemoryStream。

before using this i just need a clarification that i am doing the right thing for the right job. 在使用此功能之前,我只需要澄清一下我在为正确的工作做正确的事情。

N:B: Sometiome my clients will send 10MB+ data to the webservice which i need to decompress in webservice side. N:B:我的客户有时会将10MB以上的数据发送到Web服务,而我需要在Web服务端进行解压缩。 i have more then 200 clients running. 我有200多个客户端在运行。 and where the webservice is hosted there are more then 30 web application and webservice also hosted though my webservice is under different app pool . 尽管我的Web服务位于不同的应用程序池中,但在托管Web服务的地方还有30多个Web应用和Web服务。

I did see : GZIP decompression C# OutOfMemory 我确实看到了: GZIP解压缩C#OutOfMemory

i get some knowledge from there but for webservice which one should better i have quite confusion based on my situation. 我从那里获得了一些知识,但是对于Web服务,哪一个更好,我根据自己的情况感到相当困惑。 and i need a clear understanding about this. 我需要对此有一个清晰的了解。

Decompress method (uses MemoryStream) is below: 解压缩方法(使用MemoryStream)如下:

public static string Decompress(string compressedText)
        {
            try
            {
                byte[] gzBuffer = Convert.FromBase64String(compressedText);
                using (MemoryStream ms = new MemoryStream())
                {
                    int msgLength = BitConverter.ToInt32(gzBuffer, 0);
                    ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

                    byte[] buffer = new byte[msgLength];

                    ms.Position = 0;
                    using (GZipStream zip = new GZipStream(ms, CompressionMode.Decompress))
                    {
                        zip.Read(buffer, 0, buffer.Length);
                    }

                    return Encoding.UTF8.GetString(buffer);
                }
            }
            catch (Exception ex)
            {
                DataSyncLog.Debug(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType + "::" + System.Reflection.MethodBase.GetCurrentMethod().ToString() + ":" + ex.ToString()+" : "+ex.StackTrace);
            }

            return string.Empty;
        }

Decompress method (uses FileStream) is below: 解压缩方法(使用FileStream)如下:

public static string Decompress(string compressedText)
        {
            string SourceDirectory = System.Guid.NewGuid().ToString();
            string DestinationDirectory = System.Guid.NewGuid().ToString();
            try
            {
                File.WriteAllBytes(SourceDirectory, Convert.FromBase64String(compressedText));
                using (FileStream fd = File.Create(DestinationDirectory))
                {
                    using (FileStream fs = File.OpenRead(SourceDirectory))
                    {
                        fs.Seek(4, 0);

                        using (Stream csStream = new GZipStream(fs, CompressionMode.Decompress))
                        {

                            byte[] buffer = new byte[1024];

                            int nRead;

                            while ((nRead = csStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                fd.Write(buffer, 0, nRead);
                            }
                        }
                    }
                }

                return Encoding.UTF8.GetString(File.ReadAllBytes(DestinationDirectory));
            }
            catch (Exception ex)
            {
                DataSyncLog.Debug(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType + "::" + System.Reflection.MethodBase.GetCurrentMethod().ToString() + ":" + ex.ToString() + " : " + ex.StackTrace);
                return string.Empty;
            }
            finally
            {
                ClearFiles(SourceDirectory);
                ClearFiles(DestinationDirectory);
            }
        }

Someone could you please show me the right direction that which one should i use or any modification needed to the method that uses MemoryStream that can overcome this error. 有人可以告诉我应该使用哪个正确的方向,或者可以对使用MemoryStream的方法进行必要的修改以克服此错误。 i will be grateful to you if you give me a clear understanding about this or any code change suggestion. 如果您对我或任何代码更改建议有清晰的了解,我将不胜感激。

Working with stream looks more efficient memory-wise in the second case: with memory stream you hold entire stream in memory, with file stream you have only buffer of limited size. 在第二种情况下,使用流在内存方面看起来更加高效:使用内存流,您可以将整个流保存在内存中,而使用文件流,则只能使用有限大小的缓冲区。

Both of your methods can have problems with memory just because of their signature: when client sends 10MB this amount of memory will be allocated for compressedText argument and for return value. 您的这两种方法都可能由于其签名而导致内存问题:当客户端发送10MB内存时,该内存量将分配给CompressedText参数和返回值。

You can look into changing interface of your service, so data is transfered in chunks (here you can find example of similar approach - http://www.codeproject.com/Articles/43272/Uploading-Large-Files-Through-Web-Service ) 您可以查看服务接口的变化,以便按块传输数据(在这里您可以找到类似方法的示例-http: //www.codeproject.com/Articles/43272/Uploading-Large-Files-Through-Web-服务

Or, if you can consider switching to WCF, it supports streaming transfer mode - http://msdn.microsoft.com/en-us/library/ms751463.aspx 或者,如果您考虑切换到WCF,则它支持流传输模式-http://msdn.microsoft.com/zh-cn/library/ms751463.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM