Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 客户端计算机:在Flurl.Http.FlurlClient.ReadResponseCookies处引发NullReference异常_C#_Http_Asynchronous_Factory_Flurl - Fatal编程技术网

C# 客户端计算机:在Flurl.Http.FlurlClient.ReadResponseCookies处引发NullReference异常

C# 客户端计算机:在Flurl.Http.FlurlClient.ReadResponseCookies处引发NullReference异常,c#,http,asynchronous,factory,flurl,C#,Http,Asynchronous,Factory,Flurl,问题只发生在我的客户机上。我在4台机器上运行了我的应用程序,但没有成功复制 在使用Flurl library时,我想询问有关调试以下异常的建议和帮助: 是什么原因导致此异常仅在特定计算机上运行 System.TypeInitializationException:“Module.Performance.PriceProviders.YahooClientFactory”的类型初始值设定项引发异常。-->System.AggregateException:发生一个或多个错误。-->System.N

问题只发生在我的客户机上。我在4台机器上运行了我的应用程序,但没有成功复制

在使用Flurl library时,我想询问有关调试以下异常的建议和帮助:

是什么原因导致此异常仅在特定计算机上运行

System.TypeInitializationException:“Module.Performance.PriceProviders.YahooClientFactory”的类型初始值设定项引发异常。-->System.AggregateException:发生一个或多个错误。-->System.NullReferenceException:对象引用未设置为对象的实例。 在Flurl.Http.FlurlClient.ReadResponseCookiesHttpResponseMessageResponse处 在Flurl.Http.FlurlClient.d_u28.MoveNext -内部异常堆栈跟踪的结束-- 在System.Threading.Tasks.Task.ThrowifeExceptionalBoolean中包含TaskCanceledExceptions 在System.Threading.Tasks.Task1.GetResultCreboolean waitCompletionNotification中 在System.Threading.Tasks.Task1.get_结果中 位于Module.Performance.PriceProviders.YahooClientFactory..cctor -内部异常堆栈跟踪的结束-- 位于Module.Performance.PriceProviders.YahooClientFactory.get\u GetYahooClient 在Module.Performance.PriceProviders.YahooFlurlClient.d_u9.MoveNext --来自引发异常的上一个位置的堆栈结束跟踪-- 在System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccessTask任务中 在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotificationTask任务中 位于Module.Performance.PriceProviders.YahooStockPriceProvider.d_u0.MoveNext --来自引发异常的上一个位置的堆栈结束跟踪-- 在System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccessTask任务中 在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotificationTask任务中 在Module.Performance.PriceProviders.PriceProviderBase.d_u3.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)]));
    }
}

看起来您遇到了一个问题,Flurl在请求失败后尝试读取Cookie,而不检查空响应。这对于Flurl.http2.0是固定的。请升级到,并报告是否解决了您的问题。

感谢您的指导,我仍在进行更新,以确保它解决了我的问题。像往常一样遇到一些程序集绑定问题。升级到2.0版后问题已修复,谢谢您的提示: