我正在尋找一種方法來抑制HttpModule中的重複請求。不幸的是我不斷收到以下錯誤:我正在尋找一個解決方案,請解釋。限制重複請求
System.ObjectDisposedException: The semaphore has been disposed.
System.ObjectDisposedException: The semaphore has been disposed. at System.Threading.SemaphoreSlim.CheckDispose() at System.Threading.SemaphoreSlim.Release(Int32 releaseCount) at System.Threading.SemaphoreSlim.Release()
我的方法如下。
// Container for semaphores
private static readonly ConcurrentDictionary<string, SemaphoreSlim>
SemaphoreSlims = new ConcurrentDictionary<string, SemaphoreSlim>();
// Wrapper for getting semaphore
private static SemaphoreSlim GetSemaphoreSlim(string id)
{
return SemaphoreSlims.GetOrAdd(id, new SemaphoreSlim(1, 1));
}
private async Task ProcessImageAsync(HttpContext context)
{
// `hash` is the request path hashed.
SemaphoreSlim semaphore = GetSemaphoreSlim(hash);
await semaphore.WaitAsync();
try
{
// Do awaitable task
}
finally
{
semaphore.Release();
}
}
Dispose()
處置HTTP模塊本身的過程中,只叫(由我)。
private void Dispose(bool disposing)
{
if (this.isDisposed)
{
return;
}
if (disposing)
{
// Dispose of any managed resources here.
foreach (KeyValuePair<string, SemaphoreSlim> semaphore in SemaphoreSlims)
{
semaphore.Value.Dispose();
}
SemaphoreSlims.Clear();
}
// Call the appropriate methods to clean up
// unmanaged resources here.
// Note disposing is done.
this.isDisposed = true;
}
設置一個斷點,以查看您的dispose代碼是如何調用的以及由誰調用的。 – usr
我不得不以某種方式記錄它。拋出錯誤似乎只發生在負載和零星的情況下。 –