From 99ac770abbe3a95887c4b10c82f3985aa878eeef Mon Sep 17 00:00:00 2001 From: Oren Hurvitz Date: Sun, 1 Jun 2014 17:39:11 +0300 Subject: Close streams immediately when we finish using them --- .../Servers/HttpServer/RestSessionService.cs | 61 +++++++++++----------- 1 file changed, 30 insertions(+), 31 deletions(-) (limited to 'OpenSim/Framework/Servers/HttpServer/RestSessionService.cs') diff --git a/OpenSim/Framework/Servers/HttpServer/RestSessionService.cs b/OpenSim/Framework/Servers/HttpServer/RestSessionService.cs index 83c9848..ad69cd2 100644 --- a/OpenSim/Framework/Servers/HttpServer/RestSessionService.cs +++ b/OpenSim/Framework/Servers/HttpServer/RestSessionService.cs @@ -77,25 +77,24 @@ namespace OpenSim.Framework.Servers.HttpServer request.ContentType = "text/xml"; request.Timeout = 20000; - MemoryStream buffer = new MemoryStream(); - - XmlWriterSettings settings = new XmlWriterSettings(); - settings.Encoding = Encoding.UTF8; - - using (XmlWriter writer = XmlWriter.Create(buffer, settings)) + using (MemoryStream buffer = new MemoryStream()) { - XmlSerializer serializer = new XmlSerializer(type); - serializer.Serialize(writer, sobj); - writer.Flush(); - } + XmlWriterSettings settings = new XmlWriterSettings(); + settings.Encoding = Encoding.UTF8; - int length = (int)buffer.Length; - request.ContentLength = length; + using (XmlWriter writer = XmlWriter.Create(buffer, settings)) + { + XmlSerializer serializer = new XmlSerializer(type); + serializer.Serialize(writer, sobj); + writer.Flush(); + } + + int length = (int)buffer.Length; + request.ContentLength = length; - Stream requestStream = request.GetRequestStream(); - requestStream.Write(buffer.ToArray(), 0, length); - buffer.Close(); - requestStream.Close(); + using (Stream requestStream = request.GetRequestStream()) + requestStream.Write(buffer.ToArray(), 0, length); + } TResponse deserial = default(TResponse); using (WebResponse resp = request.GetResponse()) @@ -133,25 +132,25 @@ namespace OpenSim.Framework.Servers.HttpServer request.ContentType = "text/xml"; request.Timeout = 10000; - MemoryStream buffer = new MemoryStream(); + using (MemoryStream buffer = new MemoryStream()) + { + XmlWriterSettings settings = new XmlWriterSettings(); + settings.Encoding = Encoding.UTF8; + + using (XmlWriter writer = XmlWriter.Create(buffer, settings)) + { + XmlSerializer serializer = new XmlSerializer(type); + serializer.Serialize(writer, sobj); + writer.Flush(); + } - XmlWriterSettings settings = new XmlWriterSettings(); - settings.Encoding = Encoding.UTF8; + int length = (int)buffer.Length; + request.ContentLength = length; - using (XmlWriter writer = XmlWriter.Create(buffer, settings)) - { - XmlSerializer serializer = new XmlSerializer(type); - serializer.Serialize(writer, sobj); - writer.Flush(); + using (Stream requestStream = request.GetRequestStream()) + requestStream.Write(buffer.ToArray(), 0, length); } - buffer.Close(); - - int length = (int)buffer.Length; - request.ContentLength = length; - Stream requestStream = request.GetRequestStream(); - requestStream.Write(buffer.ToArray(), 0, length); - requestStream.Close(); // IAsyncResult result = request.BeginGetResponse(AsyncCallback, request); request.BeginGetResponse(AsyncCallback, request); } -- cgit v1.1