![](/img/trans.png)
[英]System.ObjectDisposedException: Cannot access a disposed object. AplicationDbContext
[英]Owin: Exception: System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.HttpListenerRequest'
我有很多如下日志,有人知道這的根本原因嗎? 這是否與以下代碼相關:
// Gets the client IP when hosted in IIS, where HttpContext.Current is not null.
if (httpRequest != null)
return httpRequest.UserHostAddress;
// Gets the client IP when hosted in Owin, where there is no HttpContext.Current by default.
if (!request.Properties.ContainsKey("MS_OwinContext"))
return null;
var context = request.Properties["MS_OwinContext"] as OwinContext;
return context?.Request.RemoteIpAddress;
日志:
Exception: System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.HttpListenerRequest'. at System.Net.HttpListenerRequest.CheckDisposed() at System.Net.HttpListenerRequest.get_RemoteEndPoint() at Microsoft.Owin.Host.HttpListener.RequestProcessing.OwinHttpListenerRequest.GetRemoteIpAddress() at Microsoft.Owin.Host.HttpListener.RequestProcessing.CallEnvironment.get_ServerRemoteIpAddress() at Microsoft.Owin.Host.HttpListener.RequestProcessing.CallEnvironment.PropertiesTryGetValue(String key, Object& value) at Microsoft.Owin.Host.HttpListener.RequestProcessing.CallEnvironment.TryGetValue(String key, Object& value) at Microsoft.Owin.OwinRequest.Get[T](String key)
我之前遇到過類似的問題,發現Request
上有一個CancellationToken
,您可以檢查是否已請求取消。
下面是我嘗試訪問Request (OwinRequest)
的LocalPort
並用if
包圍它的代碼。 您可以使用相同的if
。
// context.Request.LocalPort will throw ObjectDisposedException if we try to access LocalPort of cancelled
// request, thus the order of conditions in below if is important.
if (context?.Request != null &&
!context.Request.CallCancelled.IsCancellationRequested &&
context.Request.LocalPort != null)
{
requestPort = context.Request.LocalPort.Value;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.