Every time on visual studio 2015, when i run Code Analysis
, there are some annoying warnings. All of them are in a methods like this:
here is my method:
public static JObject ReadJson(string file_path)
{
try {
JObject o1 = JObject.Parse(File.ReadAllText(file_path));
using (StreamReader file = File.OpenText(file_path))
{
using (JsonTextReader reader = new JsonTextReader(file))
{
return (JObject)JToken.ReadFrom(reader);//the warning is here
}
}
}
catch
{
return default(JObject);
}
}
so why this warning occur? How to solve it? And the most important is what my fault in this method it seems to me very perfect
Warning Description
Severity Code Description Project File Line Warning CA2202 : Microsoft.Usage : Object 'file' can be disposed more than once in method 'JsonHelper.ReadJson(string)'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.