【问题标题】:Owin: Exception: System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.HttpListenerRequest'Owin:异常:System.ObjectDisposedException:无法访问已处置的对象。对象名称:'System.Net.HttpListenerRequest'
【发布时间】:2018-07-25 06:10:52
【问题描述】:

我得到了很多类似下面的日志,有人知道这件事的根本原因吗?这是否与以下代码有关:

// 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)

【问题讨论】:

  • 欢迎,但请分享登录文本。
  • 发现这个问题是由于请求在进入'context?.Request.RemoteIpAddress'步骤之前被取消引起的。
  • @zhenm 我现在也有同样的问题 - 如何检查请求是否在此步骤之前被取消?

标签: c# httprequest owin


【解决方案1】:

我之前遇到过类似的问题,发现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;
}

【讨论】:

    猜你喜欢
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多