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.Task
1.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)]));
}
}