G
Guest
LS,
The X509CertificateCollection.Remove and X509CertificateCollection.Contains
are not consistent with each other. Although a X509CertificateCollection
reports that it contains a certificate, it might not be able to remove this
certificate with exception "Cannot remove the specified item because it was
not found in the specified Collection". I believe this is not consistent with
ICollection interface / pattern. Please correct me if I am wrong.
See code snippet below:
_____________________________________
public void Reproduce( X509Certificate certificate )
{
System.Security.Cryptography.X509Certificates.X509CertificateCollection coll
= new X509CertificateCollection ();
System.Security.Cryptography.X509Certificates.X509Certificate
otherCeritificate = new X509Certificate (certificate);
coll.Add ( certificate );
System.Diagnostics.Debug.WriteLineIf ( coll.Contains ( otherCeritificate ),
"*Collection contains otherCertificate" );
try
{
coll.Remove ( otherCeritificate );
}
catch ( Exception e )
{
System.Diagnostics.Debug.WriteLine ( string.Format ( "*An exception
occured while removing otherCertificate from the collection. Message: {0}.",
e.Message ) );
}
}
_____________________________________
This prints:
*Collection contains otherCertificate
*An exception occured while removing otherCertificate from the collection.
Message: Cannot remove the specified item because it was not found in the
specified Collection..
_____________________________________
Our Fix:
// Workaround.
for ( int i = 0; i < certificateCollection.Count; i++ )
{
if ( certificateCollection.Equals ( certificate ))
{
certificateCollection.RemoveAt ( i );
break;
}
}
Not too pretty, but it works.
Regards,
Martijn Kaag
______________________________
www.VECOZO.nl
The X509CertificateCollection.Remove and X509CertificateCollection.Contains
are not consistent with each other. Although a X509CertificateCollection
reports that it contains a certificate, it might not be able to remove this
certificate with exception "Cannot remove the specified item because it was
not found in the specified Collection". I believe this is not consistent with
ICollection interface / pattern. Please correct me if I am wrong.
See code snippet below:
_____________________________________
public void Reproduce( X509Certificate certificate )
{
System.Security.Cryptography.X509Certificates.X509CertificateCollection coll
= new X509CertificateCollection ();
System.Security.Cryptography.X509Certificates.X509Certificate
otherCeritificate = new X509Certificate (certificate);
coll.Add ( certificate );
System.Diagnostics.Debug.WriteLineIf ( coll.Contains ( otherCeritificate ),
"*Collection contains otherCertificate" );
try
{
coll.Remove ( otherCeritificate );
}
catch ( Exception e )
{
System.Diagnostics.Debug.WriteLine ( string.Format ( "*An exception
occured while removing otherCertificate from the collection. Message: {0}.",
e.Message ) );
}
}
_____________________________________
This prints:
*Collection contains otherCertificate
*An exception occured while removing otherCertificate from the collection.
Message: Cannot remove the specified item because it was not found in the
specified Collection..
_____________________________________
Our Fix:
// Workaround.
for ( int i = 0; i < certificateCollection.Count; i++ )
{
if ( certificateCollection.Equals ( certificate ))
{
certificateCollection.RemoveAt ( i );
break;
}
}
Not too pretty, but it works.
Regards,
Martijn Kaag
______________________________
www.VECOZO.nl