是否有 .cs 文件等同于 .razor 文件的 @inject HttpClient Http
Is there .cs file equivalent for .razor file's @inject HttpClient Http
在我使用的 .razor 文件中
@inject HttpClient Http
访问 HTTPClient。
有没有办法在 .cs 文件中做同样的事情,还是我必须将它作为参数传递?
更新
我以为我有,但我没有。
使用语句
using System.Net.Http;
using Microsoft.AspNetCore.Components;
using System.Net.Http.Json;
定义为class参数
[Inject]
protected HttpClient Http {get;set;}
在我的通话任务中
await Http.GetFromJsonAsync<SharedGLAccount[]>($"api/{ST_comp}/GLAccounts")
出现以下错误
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Value cannot be null. (Parameter 'client')
我建议您为此使用 IHttpClientFactory
。查看 this 文档,其中解释了使用它的好处并复制如下:
- Provides a central location for naming and configuring logical
HttpClient
instances. For example, a client named github could be registered and configured to access GitHub. A default client can be registered for general access.
- Codifies the concept of outgoing middleware via delegating handlers in
HttpClient
. Provides extensions for Polly-based middleware to take advantage of delegating handlers in HttpClient
.
- Manages the pooling and lifetime of underlying
HttpClientMessageHandler
instances. Automatic management avoids common DNS (Domain Name System) problems that occur when manually managing HttpClient
lifetimes.
- Adds a configurable logging experience (via
ILogger
) for all requests sent through clients created by the factory.
一个用法示例是:
在 startup.cs 文件中:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient(); // <- add this
您可以在服务或存储库中使用它进行注入 class:
public class BasicService : IBasicService
{
private readonly IHttpClientFactory _httpClientFactory;
public BasicUsageModel(IHttpClientFactory httpClientFactory) // <- inject here
{
_httpClientFactory = httpClientFactory;
}
或者如果它的剃须刀页面代码在后面:
[Inject] public IHttpClientFactory HttpClientFactory { get; set; }
或者这个,如果它的剃刀页面:
@inject IHttpClientFactory HttpClientFactory
并像这样使用它:
var httpClient = _clientFactory.CreateClient(); // <- create HTTP client
在我使用的 .razor 文件中
@inject HttpClient Http
访问 HTTPClient。
有没有办法在 .cs 文件中做同样的事情,还是我必须将它作为参数传递?
更新
我以为我有,但我没有。
使用语句
using System.Net.Http;
using Microsoft.AspNetCore.Components;
using System.Net.Http.Json;
定义为class参数
[Inject]
protected HttpClient Http {get;set;}
在我的通话任务中
await Http.GetFromJsonAsync<SharedGLAccount[]>($"api/{ST_comp}/GLAccounts")
出现以下错误
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Value cannot be null. (Parameter 'client')
我建议您为此使用 IHttpClientFactory
。查看 this 文档,其中解释了使用它的好处并复制如下:
- Provides a central location for naming and configuring logical
HttpClient
instances. For example, a client named github could be registered and configured to access GitHub. A default client can be registered for general access.- Codifies the concept of outgoing middleware via delegating handlers in
HttpClient
. Provides extensions for Polly-based middleware to take advantage of delegating handlers inHttpClient
.- Manages the pooling and lifetime of underlying
HttpClientMessageHandler
instances. Automatic management avoids common DNS (Domain Name System) problems that occur when manually managingHttpClient
lifetimes.- Adds a configurable logging experience (via
ILogger
) for all requests sent through clients created by the factory.
一个用法示例是:
在 startup.cs 文件中:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient(); // <- add this
您可以在服务或存储库中使用它进行注入 class:
public class BasicService : IBasicService
{
private readonly IHttpClientFactory _httpClientFactory;
public BasicUsageModel(IHttpClientFactory httpClientFactory) // <- inject here
{
_httpClientFactory = httpClientFactory;
}
或者如果它的剃须刀页面代码在后面:
[Inject] public IHttpClientFactory HttpClientFactory { get; set; }
或者这个,如果它的剃刀页面:
@inject IHttpClientFactory HttpClientFactory
并像这样使用它:
var httpClient = _clientFactory.CreateClient(); // <- create HTTP client