2013-10-03 35 views
1

我有一个asp.net web服务。该web服务的一些功能是首先解压缩客户端请求。为此我写了2种方法之一使用MemoryStream和其他用途FileStreamMemoryStream VS FileStream哪一个最好在Webservice中用于GZipStream解压?

使用时MemoryStream sometiomes它得到OutofMemoryException。所以我计划使用FileStream而不是MemoryStream出于这个原因。

在使用此之前,我只需要澄清一点,我正在做正确的工作 事情。

N:B: Sometiome我的客户将10MB +数据发送到我需要在web服务端解压缩web服务。我有更多的200客户端运行。和web服务托管的地方有更多然后30 web应用程序和webservice也托管,虽然我的web服务是在不同的应用程序池

我没有看到:GZIP decompression C# OutOfMemory

我得到一些知识,从那里但是对于Web服务,人们应该更好,我必须根据我的情况相当混乱。我需要对此有清晰的认识。

解压缩方法(使用的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; 
     } 

解压缩方法(使用的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); 
      } 
     } 

有人可以请您告诉我正确的方向,我应该使用哪一个 或需要对使用 的MemoryStream的方法进行任何修改,以便克服此错误。如果您明确了解此建议或任何代码更改 建议,我将非常感谢您。

回答

0

与流工作看起来更高效的内存明智的第二种情况:与内存流,你保存在内存整个流,以文件流,你只有有限的大小的缓冲区。

你的两个方法都可能因为它们的签名而产生内存问题:当客户端发送10MB时,这个内存量将被分配给compressedText参数和返回值。

你可以考虑改变你的服务的接口,因此数据以块转移(在这里你可以发现类似的做法 - http://www.codeproject.com/Articles/43272/Uploading-Large-Files-Through-Web-Service

或者,如果你可以考虑切换到WCF,它支持流媒体传输模式 - http://msdn.microsoft.com/en-us/library/ms751463.aspx

+0

谢谢@Alexey Zuev。首先切换到WCF现在是不可能的。我不明白这个链接。我没有通过Web服务传输大文件的问题。我的问题是在处理数据时应考虑我的情况? – Rezoan

+0

可能是我已经了解了一点链接。你的意思是没有额外的回报功能。只需在Web方法内处理(解压缩)请求吧? – Rezoan