Groups | Blog | Home
all groups > dotnet web services > february 2006 >

dotnet web services : Why isn't the Dispose method of my IDisposable web service object being called?


Brad Wood <bradley|.wood| NO[at]SPAM ndsu|
2/9/2006 12:13:53 PM
If I run this console app (outside the debugger), I will see both
Console.WriteLine statements:

class Program
{
class MyClass: IDisposable
{
public void Dispose()
{
Console.WriteLine( "MyClass dispose called" );
}
}
class MyClass2 : IDisposable
{
public void Dispose()
{
Console.WriteLine( "MyClass2 dispose called" );
}
}
static void Main( string[] args )
{
using ( MyClass bob = new MyClass() )
{
using ( MyClass2 bob2 = new MyClass2() )
{
throw new Exception( "My exception" );
}
}
}
}

So why is it that the Dispose method of an object in my web service
isn't being called when it's used by a using clause where an unhandled
exception occurs?
I know Dispose isn't called because if I set a break point in Dispose it
never hits (execution goes directly to my parent WebService object's
Dispose method), and because I have an assert statement in my finalizer
reporting that Dispose wasn't called (along with
GC.SuppressFinalize(this) in the Dispose method). I see the assert
Brad Wood <bradley|.wood| NO[at]SPAM ndsu|
2/9/2006 12:32:37 PM
Sorry, disregard; the exception is occurring in the constructor itself
(not good).

[quoted text, click to view]
AddThis Social Bookmark Button