I have the following method:
public byte[] HtmlToDoc(string hmtl, string userId)
{
byte[] data;
var auditor = new ServiceAuditor
{
User = userId
};
try
{
using (var tx = new ServerText())
{
tx.Create();
tx.Load(Server.HtmlDecode(hmtl), StringStreamType.HTMLFormat);
tx.Save(out data, BinaryStreamType.MSWord);
}
}
catch (Exception e)
{
auditor.Errormessage = e.Message + "/n " + e.StackTrace;
data = new byte[0];
}
finally
{
auditor.Save();
auditor.Dispose();
}
return data;
}
and I receive the following warning during compilation:
warning CA2000: Microsoft.Reliability : In method 'DocCreator.HtmlToDoc(string, string)', object 'new ServiceAuditor()' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'new ServiceAuditor()' before all references to it are out of scope.
The weird thing is that I don't see why it is complaining even though I am disposing the object. Could you point where is the issue?
auditor.Save()
line throws an exception then? Simplest solution is to put theauditor
object inside ausing
block, then it's always going to get disposed. - DavidGauditor.Save
within thefinally
-block itself can throw an exception. ThusDispose
won´t be called. - HimBromBeere