0

Problem occurs only on my client machine. I ran my application on 4 machines without reproduction success.

I would like to ask on advice and help in debug the following exception while using Flurl library:

What may cause this exception to run only on specific machine ?

System.TypeInitializationException: The type initializer for 'Module.Performance.PriceProviders.YahooClientFactory' threw an exception. ---> System.AggregateException: One or more errors occurred. ---> System.NullReferenceException: Object reference not set to an instance of an object. at Flurl.Http.FlurlClient.ReadResponseCookies(HttpResponseMessage response) at Flurl.Http.FlurlClient.d__28.MoveNext() --- End of inner exception stack trace --- at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task1.get_Result() at Module.Performance.PriceProviders.YahooClientFactory..cctor() --- End of inner exception stack trace --- at Module.Performance.PriceProviders.YahooClientFactory.get_GetYahooClient() at Module.Performance.PriceProviders.YahooFlurlClient.d__9.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Module.Performance.PriceProviders.YahooStockPriceProvider.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Module.Performance.PriceProviders.PriceProviderBase.d__3.MoveNext()

Code beneath the exception looks like:

YahooClientFactory is singletone factor to optimize the request time. Factory is often called by other async task (higher layer)

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)]));
    }
}
Patryk
  • 39
  • 7

1 Answers1

1

It looks like you've encountered a 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 and report back whether that resolves your issue.

Todd Menier
  • 37,557
  • 17
  • 150
  • 173
  • Thank you for guidance, I'm still working on updates to assure it's solve my issue. Ran into some assembly binding issues (as always). – Patryk Oct 03 '17 at 14:00
  • Issue fixed after upgrade to version 2.0, Thank you for your tip :) – Patryk Oct 06 '17 at 14:21