Reputation: 352
I am getting code analysis error in the below method.
public static OracleCommand CreateStoredProcedureCommand(string name,
OracleConnection connection)
{
return new OracleCommand(name, connection) { CommandType = CommandType.StoredProcedure };
}
CA2000 : Microsoft.Reliability : In method 'StoredProcedureHelper.CreateStoredProcedureCommand(string, OracleConnection)', object 'command' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'command' before all references to it are out of scope
how can it this be resolved without suppressing this?
Upvotes: 1
Views: 2709
Reputation: 23324
The object is not disposed, when the assignment to the property throws an exception. Try this:
public static OracleCommand CreateStoredProcedureCommand(string name,
OracleConnection connection)
{
OracleCommand result = new OracleCommand(name, connection);
try
{
result.CommandType = CommandType.StoredProcedure;
return result;
}
catch
{
result.Dispose();
throw;
}
}
Upvotes: 7
Reputation: 19702
It can't, looking at the method, the responsibility for disposing of the object must always lie with the caller.
You will have to suppress it.
Upvotes: 0