2

最近开始使用 ReactiveUI,到目前为止一切都很好。在尝试找出问题时,我遇到了一个HttpClient / Refit问题。我有一个很长的列表,该列表中的每个项目都必须向 API 发出请求,如下所示。

这是一个反应对象

public SearchResult()
{
    LoadStats = ReactiveCommand.CreateAsyncTask(_ => DownloadMultipleCalls(this.IdText, this.DisplayText));

    LoadStats
        .ThrownExceptions
        .SubscribeOn(RxApp.MainThreadScheduler)
        .Subscribe(ex => UserError.Throw("Couldn't load stats", ex));

    _avaText = LoadStats.Select(x => x.Ava).ToProperty(this, x => x.AvaText, string.Empty);
    _perText = LoadStats.Select(x => x.Per).ToProperty(this, x => x.PerText, string.Empty);

    this.WhenAnyValue(x => x.IdText)
        .Where(x => !string.IsNullOrWhiteSpace(x))
                //.Select(_ => Observable.Timer(DateTimeOffset.MinValue, TimeSpan.FromSeconds(1), RxApp.MainThreadScheduler))
        .Retry(5)
        .InvokeCommand(LoadStats);
}

在 DownloadMultipleCalls 中发生了以下情况。

async static Task<AvaPer> DownloadMultipleCalls(string id, string displaytext)
{
    AvaPer response = new AvaPer();
    var block = new ActionBlock<string>(async service =>
    {
        response = await DownloadCall(service, displaytext);
    },
    new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 4 });

    block.Post(id);

    block.Complete();
    await block.Completion;

    return response;
}

async static Task<AvaPer> DownloadCall(string id, string displaytext)
{
    System.Diagnostics.Debug.WriteLine($"Starting download at {DateTimeOffset.Now}");

    var client = new HttpClient(NetCache.UserInitiated)
    {
        BaseAddress = new Uri("https://api.adres.com"),
    };

    //client.Timeout = TimeSpan.FromMilliseconds(10);

    var token = GetToken;

    client.DefaultRequestHeaders.Add("authToken", token);
    var api = RestService.For<IMyApi>(client);

    AvaPer idEntries = new AvaPer();
    try
    {
        var searchResult = await api.GetAvaPerStatusRaw(id); // <== Here is the error (400 - Bad Request)

        //var parsedEntries = Task.Run(() => {
        idEntries = new AvaPer
        {
            Id = searchResult.Id.ToString() ?? string.Empty,
            display = displaytext ?? string.Empty,
            Ava = searchResult.AvailabilityStatus ?? string.Empty,
            Per = searchResult.PerformanceStatus ?? string.Empty,
            Updated = DateTimeOffset.Now.ToLocalTime().ToString() ?? string.Empty
        };
        //}).ConfigureAwait(false);

        //return idEntries;
    }
    catch (ApiException ex)
    {
        var statusCode = ex.StatusCode;
        var error = ex.GetContentAs<Models.ServiceModel.ErrorResponse>();
    }

    return idEntries;
}

最后,这行代码出错了var searchResult = await api.GetAvaPerStatusRaw(id);应用程序崩溃并响应“400 Bad Request”,如果我查看错误,它说没有 id 值代替 GET 请求。但是当我检查id 有一个值的初始值时,它不应该对此做出响应。谁能帮我这个?如果您有任何问题,请随时提问。非常感谢。

亲切的问候,

费尔南多

4

0 回答 0