c# - Code analysis complains that object can be disposed more than once. Why? -
i 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 function places like 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 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 post-string 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(); // actual request , read 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; }
streamreader takes ownership of stream passed in constructor call in sense call dispose on when streamreader closed - hence disposed when outer using statement attempts dispose of it.
Comments
Post a Comment