客户端机器:在 Flurl.Http.FlurlClient.ReadResponseCookies 抛出 NullReference 异常
Client Machine : throw NullReference Exception at Flurl.Http.FlurlClient.ReadResponseCookies
问题只发生在我的客户端机器上。我运行我的应用在4台机器上都没有重现成功。
我想在使用 Flurl 库时寻求建议和帮助调试以下异常:
什么可能导致此异常 运行 仅在特定机器上?
System.TypeInitializationException:'Module.Performance.PriceProviders.YahooClientFactory' 的类型初始值设定项抛出异常。 ---> System.AggregateException: 发生一个或多个错误。 ---> System.NullReferenceException: 对象引用未设置为对象的实例。
在 Flurl.Http.FlurlClient.ReadResponseCookies(HttpResponseMessage 响应)
在 Flurl.Http.FlurlClient.d__28.MoveNext()
--- 内部异常堆栈跟踪结束 ---
在 System.Threading.Tasks.Task.ThrowIfExceptional(布尔值 includeTaskCanceledExceptions)
在 System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task
1.get_Result()
在 Module.Performance.PriceProviders.YahooClientFactory..cctor()
--- 内部异常堆栈跟踪结束 ---
在 Module.Performance.PriceProviders.YahooClientFactory.get_GetYahooClient()
在 Module.Performance.PriceProviders.YahooFlurlClient.d__9.MoveNext()
--- 从抛出异常的先前位置开始的堆栈跟踪结束 ---
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)
在 Module.Performance.PriceProviders.YahooStockPriceProvider.d__0.MoveNext()
--- 从抛出异常的先前位置开始的堆栈跟踪结束 ---
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)
在 Module.Performance.PriceProviders.PriceProviderBase.d__3.MoveNext()
异常下方的代码如下所示:
YahooClientFactory 是优化请求时间的单例因素。工厂经常被其他异步任务(更高层)调用
public static class YahooClientFactory
{
public static IFlurlClient GetYahooClient => YahooClientInstance;
public static string Crumb { get; }
private static readonly IFlurlClient YahooClientInstance;
private const string CookieUrl = "https://finance.yahoo.com";
private static string userAgent = "User-Agent";
private static string headerString =
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
private const string CrumbUrl = "https://query1.finance.yahoo.com/v1/test/getcrumb";
private static int MaxRetryCount = 5;
static YahooClientFactory()
{
YahooClientInstance = new FlurlClient()
.WithHeader(userAgent, headerString)
.EnableCookies()
.WithUrl($"{CookieUrl}?{GetRandomString(8)}");
for (int i = 0; i < MaxRetryCount; i++)
{
YahooClientInstance
.GetAsync(CancellationToken.None)
.Result
.EnsureSuccessStatusCode();
if (YahooClientInstance.Cookies?.Count > 0)
{
break;
}
if (i == MaxRetryCount)
{
throw new Exception("Reached maximum number of retries when connecting to yahoo client.");
}
Thread.Sleep(100);
}
Crumb = YahooClientInstance
.WithUrl(CrumbUrl)
.GetAsync(CancellationToken.None)
.ReceiveString()
.Result;
}
public static string GetRandomString(int length)
{
const string Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
return string.Join("", Enumerable.Range(0, length).Select(i => Chars[new Random().Next(Chars.Length)]));
}
}
您似乎遇到了 known bug where Flurl attempts to read cookies after a failed request without checking for a null response. This is fixed for Flurl.Http 2.0. Please upgrade to the latest prerelease 并报告是否解决了您的问题。
问题只发生在我的客户端机器上。我运行我的应用在4台机器上都没有重现成功。
我想在使用 Flurl 库时寻求建议和帮助调试以下异常:
什么可能导致此异常 运行 仅在特定机器上?
System.TypeInitializationException:'Module.Performance.PriceProviders.YahooClientFactory' 的类型初始值设定项抛出异常。 ---> System.AggregateException: 发生一个或多个错误。 ---> System.NullReferenceException: 对象引用未设置为对象的实例。
在 Flurl.Http.FlurlClient.ReadResponseCookies(HttpResponseMessage 响应)
在 Flurl.Http.FlurlClient.d__28.MoveNext()
--- 内部异常堆栈跟踪结束 ---
在 System.Threading.Tasks.Task.ThrowIfExceptional(布尔值 includeTaskCanceledExceptions)
在 System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task
1.get_Result()
在 Module.Performance.PriceProviders.YahooClientFactory..cctor()
--- 内部异常堆栈跟踪结束 ---
在 Module.Performance.PriceProviders.YahooClientFactory.get_GetYahooClient()
在 Module.Performance.PriceProviders.YahooFlurlClient.d__9.MoveNext()
--- 从抛出异常的先前位置开始的堆栈跟踪结束 ---
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)
在 Module.Performance.PriceProviders.YahooStockPriceProvider.d__0.MoveNext()
--- 从抛出异常的先前位置开始的堆栈跟踪结束 ---
在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)
在 Module.Performance.PriceProviders.PriceProviderBase.d__3.MoveNext()
异常下方的代码如下所示:
YahooClientFactory 是优化请求时间的单例因素。工厂经常被其他异步任务(更高层)调用
public static class YahooClientFactory
{
public static IFlurlClient GetYahooClient => YahooClientInstance;
public static string Crumb { get; }
private static readonly IFlurlClient YahooClientInstance;
private const string CookieUrl = "https://finance.yahoo.com";
private static string userAgent = "User-Agent";
private static string headerString =
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
private const string CrumbUrl = "https://query1.finance.yahoo.com/v1/test/getcrumb";
private static int MaxRetryCount = 5;
static YahooClientFactory()
{
YahooClientInstance = new FlurlClient()
.WithHeader(userAgent, headerString)
.EnableCookies()
.WithUrl($"{CookieUrl}?{GetRandomString(8)}");
for (int i = 0; i < MaxRetryCount; i++)
{
YahooClientInstance
.GetAsync(CancellationToken.None)
.Result
.EnsureSuccessStatusCode();
if (YahooClientInstance.Cookies?.Count > 0)
{
break;
}
if (i == MaxRetryCount)
{
throw new Exception("Reached maximum number of retries when connecting to yahoo client.");
}
Thread.Sleep(100);
}
Crumb = YahooClientInstance
.WithUrl(CrumbUrl)
.GetAsync(CancellationToken.None)
.ReceiveString()
.Result;
}
public static string GetRandomString(int length)
{
const string Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
return string.Join("", Enumerable.Range(0, length).Select(i => Chars[new Random().Next(Chars.Length)]));
}
}
您似乎遇到了 known bug where Flurl attempts to read cookies after a failed request without checking for a null response. This is fixed for Flurl.Http 2.0. Please upgrade to the latest prerelease 并报告是否解决了您的问题。