3

I was doing a quick demo to show dispose in a using bracket, but somehow managed to confuse myself. Here is my simple test:

public class CommonSqlObject : IDisposable
{
    public SqlDataReader Reader { get; set; }
    public SqlCommand Command { get; set; }
    public void Dispose()
   {
    if (Reader != null)
    {
        Reader.Close(); // <- too easy to forget
        Reader.Dispose(); // <- too easy to forget
    }
    if (Command != null)
    {
        Command.Dispose(); // <- too easy to forget
    }
}
}
public class Program
{
    static void Main(string[] args)
    {
        test();
    }

    public static void test()
    {
    using (CommonSqlObject obj = new CommonSqlObject())
    {

        SqlCommand cmd = new SqlCommand("hello");
        SqlConnection conn = new SqlConnection("data source=some server;initial catalog=somedb;user id=someuser;Min Pool Size=25;Max Pool Size=300;TimeOut=30;TransparentNetworkIPResolution=False;TrustServerCertificate=true");
        cmd.Connection = conn;
        obj.Command = cmd;
        var SqlDataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
        obj.Reader = SqlDataReader;

    }
}

}

Basically it is a fake server and I didn't open connection, so exception will be thrown in this console app. I thought if I put a break point in my Dispose method, my visual studio should hit the breakpoint and then exit the console program.

However, in my vs 2019, it just throws exception and exit, and there is no evidence that my dispose method was indeed triggered (as my breakpoint was not hit). Is this something I am doing wrongly or is it because it is a console app?

The given answer is correct, and to be bit more precise, if this code is in a web app, then in my visual studio, I do see dispose get called. In console, it didn't (as explained in the answer).

daxu
  • 3,514
  • 5
  • 38
  • 76
  • 2
    In the code example, you have shown, you are not initializing the Reader and Command member variables, so they will be null? Depending on where you put the breakpoint (inside the if statement), I would not expect them to be hit. – Mansoor Dec 03 '21 at 12:22
  • @JohnathanBarclay what if the exception happens before that point. – Mansoor Dec 03 '21 at 12:24
  • @Mansoor yes, good point. – Johnathan Barclay Dec 03 '21 at 12:25
  • Your code works fine for me - the `Dispose` method is called as expected. As Mansoor said, you've probably put your breakpoint on a line that doesn't get hit. Try adding `Console.WriteLine` calls to the method to show that it's being called. – Richard Deeming Dec 03 '21 at 12:26
  • 1
    I tried to reproduce this, but failed. `Dispose` is executed and a breakpoint is hit by the debugger. If I put the code in a try/catch block, `Dispose` is execute d before the catch block (as expected). – René Vogt Dec 03 '21 at 12:26
  • 1
    To clarify, what you probably want is a constructor for `CommonSqlObject` which accepts `SqlCommand` and `SqlConnection` as arguments (& verifies that they are valid). – Mansoor Dec 03 '21 at 12:31

0 Answers0