簡體   English   中英

Owin:異常:System.ObjectDisposedException:無法訪問已處置的 object。Object 名稱:'System.Net.HttpListenerRequest'

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM