katit
katit

Reputation: 17913

Code analysis complains that object can be disposed more than once. Why?

I get warning on responseStream in following function:

private static string GetResponseString(WebResponse response)
        {
            using (var responseStream = response.GetResponseStream())
            {
                if (responseStream != null)
                {
                    using (var responseReader = new StreamReader(responseStream))
                    {
                        var strResponse = responseReader.ReadToEnd();
                        return strResponse;
                    }
                }
            }

            return string.Empty;
        }

I call this function from places like like this one:

var request = (HttpWebRequest)WebRequest.Create(Uri);
            request.Headers.Add("Authorization", "GoogleLogin auth=" + this.SecurityToken);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Method = "POST";
            request.Timeout = 5000;

            // build the post string     
            var postString = new StringBuilder();
            postString.AppendFormat("registration_id={0}", recipientId);
            postString.AppendFormat("&data.payload={0}", message);
            postString.AppendFormat("&collapse_key={0}", collapseKey);

            // write the post-string as a byte array     
            var requestData = Encoding.ASCII.GetBytes(postString.ToString());
            request.ContentLength = requestData.Length;
            var requestStream = request.GetRequestStream();
            requestStream.Write(requestData, 0, requestData.Length);
            requestStream.Close();

            // Do the actual request and read the response stream  
            try
            {
                var response = request.GetResponse();
                var responseString = GetResponseString(response);
                response.Close();

                return responseString.Contains("id=") 
                    ? SendStatus.Ok 
                    : GetSendStatusFromResponse(responseString);
            }
            catch (WebException ex)
            {
                var webResponse = (HttpWebResponse)ex.Response;
                if (webResponse != null)
                {
                    if (webResponse.StatusCode.Equals(HttpStatusCode.Unauthorized))
                    {
                        return SendStatus.Unauthorized;
                    }

                    if (webResponse.StatusCode.Equals(HttpStatusCode.ServiceUnavailable))
                    {
                        return SendStatus.ServiceUnavailable;
                    }
                }

                this.LoggerService.Log(null, ex);
                return SendStatus.GeneralException;
            }

Upvotes: 4

Views: 1233

Answers (1)

StreamReader takes ownership of the stream passed to it in the constructor call in the sense that it will call Dispose on it when the StreamReader itself is closed - hence it will already be disposed when the outer Using statement attempts to dispose of it.

Upvotes: 5

Related Questions