客户端机器:在Flurl.Http.FlurlClient.ReadResponseCookies

时间:2017-10-02 12:31:35

标签: c# http asynchronous factory flurl

仅在我的客户端计算机上出现问题。我在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(Boolean includeTaskCanceledExceptions)    在System.Threading.Tasks.Task 1.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)]));
    }
}

1 个答案:

答案 0 :(得分:1)

您似乎遇到了known bug,其中Flurl在请求失败后尝试读取Cookie而未检查空响应。对于Flurl.Http 2.0,这是固定的。请升级到latest prerelease并报告是否能解决您的问题。