I'm in the process of doing some code analysis on a project and implementing the suggestions that make sense. One suggestion is to do the following:
CA2000 : Microsoft.Reliability : In method 'Service.ParseConfigurationFile()', call System.IDisposable.Dispose on object 'new SecureString()' before all references to it are out of scope
The offending line is as follows:
Password = me.Password.Aggregate(new SecureString(), (secureString, c) => { secureString.AppendChar(c); return secureString; })
Any ideas on how to do this properly? I have replaced the above with the line below but I don't think it's correct as it still causes the code analysis message to come up:
Password = me.Password.Aggregate(new SecureString(), (secureString, c) => { using (secureString) {secureString.AppendChar(c); return secureString;} })
EDIT: As per comment from @Jon below objectInstance is an instance of a custom class called MailboxElement (me). It's going through multiple custom sections in a config file which looks as follows:
foreach (MailboxElement me in mailboxesSection.Mailboxes)
{
MailboxInformation mailboxInformation = new MailboxInformation
{
ExchangeServerWebServiceUrl = me.ExchangeServerWebServiceUrl,
MailboxFriendlyName = me.FriendlyName,
UserName = me.UserName,
Password = me.Password.Aggregate(new SecureString(), (secureString, c) => { secureString.AppendChar(c); return secureString; }),
MailboxToAccess = me.MailboxToAccess
};
// Do stuff with mailboxInformation here
}
MailboxElement
is a sealed class that implements ConfigurationElement that has all the properties noted above.
MailboxInformation
is defined as follows:
public class MailboxInformation
{
public string MailboxFriendlyName { get; set; }
public string UserName { get; set; }
public SecureString Password { get; set; }
public string ExchangeServerWebServiceUrl { get; set; }
public string MailboxToAccess { get; set; }
public string InboxFolderId { get; set; }
public string SentItemsFolderId { get; set; }
public bool MailboxSettingsDiscovered { get; set; }
}
I hope this makes things clearer...