简体   繁体   English

如何在 Blazor 客户端应用程序的服务中调用 HttpClient

[英]How Do I Call HttpClient in Service for a Blazor Client-Side App

I want to make Http calls from a service in Blazor rather than making calls in the @code block in the .razor file nor in a code-behind.我想从 Blazor 中的服务进行 Http 调用,而不是在.razor文件中的@code块或代码隐藏中进行调用。 I receive the error:我收到错误:
Shared/WeatherService.cs(16,17): error CS0246: The type or namespace name 'HttpClient' could not be found (are you missing a using directive or an assembly reference?)

The documentation shows that this is how it's done.文档显示这如何完成的。

Complex services might require additional services.复杂的服务可能需要额外的服务。 In the prior example, DataAccess might require the HttpClient default service.在前面的示例中,DataAccess 可能需要 HttpClient 默认服务。 @inject (or the InjectAttribute) isn't available for use in services. @inject(或 InjectAttribute)不可用于服务。 Constructor injection must be used instead.必须改用构造函数注入。 Required services are added by adding parameters to the service's constructor.通过向服务的构造函数添加参数来添加所需的服务。 When DI creates the service, it recognizes the services it requires in the constructor and provides them accordingly.当 DI 创建服务时,它会在构造函数中识别它需要的服务并相应地提供它们。

Source: https://docs.microsoft.com/en-us/aspnet/core/blazor/dependency-injection?view=aspnetcore-3.0#use-di-in-services来源: https : //docs.microsoft.com/en-us/aspnet/core/blazor/dependency-injection?view=aspnetcore-3.0#use-di-in-services

How do I remedy the error?我该如何纠正错误?

// WeatherService.cs
using System.Threading.Tasks;

namespace MyBlazorApp.Shared
{
    public interface IWeatherService
    {
        Task<Weather> Get(decimal latitude, decimal longitude);
    }

    public class WeatherService : IWeatherService
    {
        public WeatherService(HttpClient httpClient)
        {
            ...
        }

        public async Task<Weather> Get(decimal latitude, decimal longitude)
        {
            // Do stuff
        }

    }
}
// Starup.cs
using Microsoft.AspNetCore.Components.Builder;
using Microsoft.Extensions.DependencyInjection;
using MyBlazorApp.Shared;

namespace MyBlazorApp
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<IWeatherService, WeatherService>();
        }

        public void Configure(IComponentsApplicationBuilder app)
        {
            app.AddComponent<App>("app");
        }
    }
}

You are missing using System.Net.Http;您缺少using System.Net.Http; to have access to the class in WeatherService.cs访问WeatherService.cs的类

// WeatherService.cs
using System.Threading.Tasks;
using System.Net.Http; //<-- THIS WAS MISSING

namespace MyBlazorApp.Shared {
    public interface IWeatherService {
        Task<Weather> Get(decimal latitude, decimal longitude);
    }

    public class WeatherService : IWeatherService {
        private HttpClient httpClient;

        public WeatherService(HttpClient httpClient) {
            this.httpClient = httpClient;
        }

        public async Task<Weather> Get(decimal latitude, decimal longitude) {
            // Do stuff
        }

    }
}

If using the full name for the class System.Net.Http.HttpClient does not work then you are definitely missing a reference to the assembly.如果使用System.Net.Http.HttpClient类的全名不起作用,那么您肯定缺少对程序集的引用。

You can configure httpclient in startup.cs.您可以在 startup.cs 中配置 httpclient。

    services.AddHttpClient();
    services.AddScoped<HttpClient>();

Now you can use HttClient in .razor files.现在您可以在 .razor 文件中使用 HttClient。

@inject HttpClient httpClient

-------------

private async Task LoadSystems() => systemsList = await httpClient.GetJsonAsync<List<Models.Systems>>("Systems/GetSystems");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM