Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I ran Analyze > Run Code Analysis on Solution on my C# Winforms app. I got this:

CA1001 Types that own disposable fields should be disposable Implement IDisposable on 'ExceptionLoggingService' because it creates members of the following IDisposable types: 'StreamWriter'. If 'ExceptionLoggingService' has previously shipped, adding new members that implement IDisposable to this type is considered a breaking change to existing consumers.

What I have tried:

So I added it:

C#
public class ExceptionLoggingService : IDisposable
{
	. . .

	public void Dispose()
        {
            _streamWriter.Dispose();
        }
        . . .


...but running Analyze > Run Code Analysis on Solution again gives me the following fingerwag:

CA1063 Implement IDisposable correctly Provide an overridable implementation of Dispose(bool) on 'ExceptionLoggingService' or mark the type as sealed. A call to Dispose(false) should only clean up native resources. A call to Dispose(true) should clean up both managed and native resources.


So what must I do to appease the beast?
Posted
Updated 18-Feb-16 11:51am
v2
Comments
BillWoodruff 18-Feb-16 17:12pm    
Do you have a first-born, or a black chicken ?
B. Clay Shannon 18-Feb-16 17:18pm    
I did have a firstborn.
Beginner Luck 18-Feb-16 17:23pm    
try using method
B. Clay Shannon 18-Feb-16 17:25pm    
All down but nine, pard; set 'em up on the other alley.
Philippe Mori 18-Feb-16 21:32pm    
By the way, if you are using Visual Studio 2015, the IDE allows you to implement the interface according to the full pattern (or an empty implementation) so you don't even have to search very far...

Have you read this: Dispose Pattern[^]?
The message says it: make your class sealed or follow the Dispose Pattern as described in the link above.
Cheers
Andi
 
Share this answer
 
v2
Thanks to Andi's link, this works:

C#
public void Dispose()
{
    Dispose(true);
    GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
    if (disposing)
    {
        if (_streamWriter != null) _streamWriter.Dispose();
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900