【问题标题】:InvalidOperationException: Cannot provide a value for property '_clientFactory' on type 'CoronaAppCsarp.Pages.Index'InvalidOperationException:无法为“CoronaAppCsarp.Pages.Index”类型的属性“_clientFactory”提供值
【发布时间】:2023-03-14 14:55:01
【问题描述】:

我正在使用这个 api 创建这个 Corona 应用程序:https://covid19.mathdro.id/api。我已经为卡片创建了一个组件来显示来自 api 的数据,当我运行应用程序时,它给了我这个错误:

我正在使用 System.Net.Http.Json。

这是我的模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CoronaAppCsarp.Data
{
        public class CoronaModel
        {
            public Confirmed confirmed { get; set; }
            public Recovered recovered { get; set; }
            public Deaths deaths { get; set; }
            public string dailySummary { get; set; }
            public string image { get; set; }
            public DateTime lastUpdate { get; set; }
        }

        public class Confirmed
        {
            public int value { get; set; }
            public string detail { get; set; }
        }

        public class Recovered
        {
            public int value { get; set; }
            public string detail { get; set; }
        }

        public class Deaths
        {
            public int value { get; set; }
            public string detail { get; set; }
        }
    }

我的卡片组件:

<div class="card" style="width: 18rem;">
    <div class="card-body">
        <h5 class="card-title">@Title</h5>
        <p class="card-text">@NumberOfCases</p>
        <p class="card-text">@Date</p>
        <p class="card-text">@Description.</p>
    </div>
</div>

@code {
    [Parameter]
    public string Title { get; set; }

    [Parameter]
    public float NumberOfCases { get; set; }

    [Parameter]
    public DateTime Date { get; set; }

    [Parameter]
    public string Description { get; set; }
}

还有我的索引页面:

@page "/"
@inject IHttpClientFactory _clientFactory;

@if (string.IsNullOrWhiteSpace(errorString) == false)
{
    <div class="h2">@errorString</div>
}
else if (coronaModel is null)
{
    <div class="h2">Loading...</div>
}
else
{
    <div class="container-fluid">
        <div class="row">
            <div class="col">
                <Cards NumberOfCases="@coronaModel.confirmed.value" Date="@coronaModel.lastUpdate" Description="Number of active COVID-19 cases" Title="Infected" />
            </div>
            <div class="col">
                <Cards NumberOfCases="@coronaModel.recovered.value" Date="@coronaModel.lastUpdate" Description="Number recovered COVID-19 cases" Title="Recovered" />
            </div>
            <div class="col">
                <Cards NumberOfCases="@coronaModel.deaths.value" Date="@coronaModel.lastUpdate" Description="Number of COVID-19 deaths" Title="Deaths" />
            </div>
        </div>
    </div>
}

@code{
    CoronaModel coronaModel;
    string errorString;

    protected override async Task OnInitializedAsync()
    {
        var request = new HttpRequestMessage(HttpMethod.Get, "https://covid19.mathdro.id/api");

        var client = _clientFactory.CreateClient();

        HttpResponseMessage response = await client.SendAsync(request);

        if (response.IsSuccessStatusCode)
        {
            coronaModel = await response.Content.ReadFromJsonAsync<CoronaModel>();
            errorString = null;
        }
        else
        {
            errorString = "There was an error getting the data.";
        }
    }

}

有什么办法可以解决吗?如果是,我将非常感谢您的帮助!

谢谢!

【问题讨论】:

    标签: c# api asp.net-core blazor blazor-server-side


    【解决方案1】:

    您应该将IHttpClientFactory 服务添加到启动类的DI 容器中。 IHttpClientFactory 可以像这样调用 AddHttpClient 来注册:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddHttpClient();
        //..................
    }
    

    【讨论】:

    • 这样一个菜鸟的错误,我好尴尬。无论如何,这解决了它,所以谢谢!
    猜你喜欢
    • 2021-04-07
    • 2022-12-15
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多