我发现我维护的一些代码存在问题。下面的代码有一个private static SHA1
成员(它是一个IDisposable
,但由于它是static
,因此永远不应该被完成)。然而,在压力下,这段代码会抛出一个异常,表明它已经关闭:
Caught exception. Safe handle has been closed"
Stack trace: Call stack where exception was thrown
at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
at System.Security.Cryptography.Utils.HashData(SafeHashHandle hHash, Byte[] data, Int32 cbData, Int32 ibStart, Int32 cbSize)
at System.Security.Cryptography.Utils.HashData(SafeHashHandle hHash, Byte[] data, Int32 ibStart, Int32 cbSize)
at System.Security.Cryptography.HashAlgorithm.ComputeHash(Byte[] buffer)
涉及的代码如下:
internal class TokenCache
{
private static SHA1 _sha1 = SHA1.Create();
private string ComputeHash(string password)
{
byte[] passwordBytes = UTF8Encoding.UTF8.GetBytes(password);
return UTF8Encoding.UTF8.GetString(_sha1.ComputeHash(passwordBytes));
}
我显然的问题是,这个问题可能是由什么引起的。调用SHA1.Create
会悄无声息地失败吗(有多少密码资源可用)?这可能是应用程序域崩溃引起的吗?
还有其他理论吗?
使用
foreach`却不行。 - Rob