【问题标题】:How Do I Call HttpClient in Service for a Blazor Client-Side App如何在 Blazor 客户端应用程序的服务中调用 HttpClient
【发布时间】:2019-12-12 01:46:47
【问题描述】:

我想从 Blazor 中的服务进行 Http 调用,而不是在 .razor 文件中的 @code 块或代码隐藏中进行调用。我收到错误:
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?)

文档显示这它是如何完成的。

复杂的服务可能需要额外的服务。在之前 例如,DataAccess 可能需要 HttpClient 默认服务。 @inject(或 InjectAttribute)不可用于服务。 必须改用构造函数注入。所需的服务是 通过向服务的构造函数添加参数来添加。当 DI 创建服务时,它会识别它在 构造函数并相应地提供它们。

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

如何纠正错误?

// 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");
        }
    }
}

【问题讨论】:

  • 您缺少using System.Net.Http; 无法访问WeatherService.cs 中的课程
  • 要明确客户端或服务器端的 Blazor,对于 HttpClient 有一些细微的差别。
  • @HenkHolterman 你能详细说明你在问什么吗?出于这个原因,“客户端”在我的问题的标题中,特别是如果我理解你的意思的话。我错过了什么吗?
  • 不,我只是错过了标题中的内容。正在查看文本和标签。

标签: c# blazor blazor-client-side


【解决方案1】:

您缺少using System.Net.Http; 以访问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
        }

    }
}

如果使用类的全名 System.Net.Http.HttpClient 不起作用,那么您肯定缺少对程序集的引用。

【讨论】:

  • 不幸的是,这并没有改变错误。此外,文档应说明是否确实如此。
  • @dapperdan1985 如果使用类的全名System.Net.Http.HttpClient 不起作用,那么您肯定缺少对程序集的引用。
  • 只需要添加using System.Net.Http;。当这不起作用时,项目会以其他方式损坏。 &lt;PackageReference 不应该是必需的,也不是一个好主意。
  • Visual Studio 显示存在其他错误,但使用dotnet build 构建仅显示...HttpClient could not be found... 错误。使用 Visual Studio(而不是 VS Code)更有效地发现了真正的错误。我想我应该不那么固执,在 Windows 而不是 Ubuntu 上开发,这样我就可以拥有完整的 Visual Studio :)
  • @dapperdan1985 很高兴您找到了解决方案。快乐编码。
【解决方案2】:

可以在startup.cs中配置httpclient。

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

现在您可以在 .razor 文件中使用 HttClient。

@inject HttpClient httpClient

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

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

【讨论】:

    猜你喜欢
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 2021-10-29
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多