简体   繁体   中英

CA2000 : Microsoft.Reliability object is not disposed along all exception paths

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?

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;
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM