gpt4 book ai didi

Using HttpClientFactory in webAPI controller (.NET core 6 )(在WebAPI控制器(.NET core 6)中使用HttpClientFactory)

转载 作者:bug小助手 更新时间:2023-10-25 19:47:42 29 4
gpt4 key购买 nike



I'm wondering if my implementation in my webAPI is correct:

我想知道我在WebAPI中的实现是否正确:


Startup.cs => ConfigureServices

Startup.cs=>ConfigureServices


services.AddHttpClient("MyHttpClient")

Startup.cs => Configure

Startup.cs=>配置


applicationLifetime.ApplicationStopping.Register(() => {
MyController.httpClient.Dispose();
});

Controller

控制器


private static IHttpClientFactory _httpClientFactory;
public static HttpClient httpClient = null;

public MyController(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;

if (httpClient == null)
{
httpClient = _httpClientFactory.CreateClient("MyHttpClient");
}
}

Methods

方法


[HttpGet]
[Consumes("application/x-www-form-urlencoded")]
public async void MyMethod1() {
await httpClient.SendAsync()
....
}


[HttpGet]
[Consumes("application/x-www-form-urlencoded")]
public async void MyMethod2() {
await httpClient.SendAsync()
....
}

更多回答

Can't really see a major problem in your implementation. I would just suggest that you use Task rather than void in the API controller actions

我真的看不出您的实现中有什么大问题。我只建议您在API控制器操作中使用任务,而不是VALID

Does code run if it is not run as a services? Usually code fails as a service unless you start the service as a Service Account. A service defaults to a System Account which doesn't have an Environment and often will fail. Do you see the service running in Task Manager? If you do not see it running it probably terminated. Check Event Viewer to see if there are any error indicating failure.

如果代码不是作为服务运行的,它是否会运行?通常,代码作为服务失败,除非您将服务作为服务帐户启动。服务默认为没有环境的系统帐户,并且通常会失败。您是否看到该服务正在任务管理器中运行?如果您没有看到它在运行,它可能会被终止。检查事件查看器,查看是否有任何指示失败的错误。

优秀答案推荐


I'm wondering if my implementation in my webAPI is correct



Well, based on your shared code snippet apparently seems that implementations would work. However, you can improve it in few areas. For instance, you have made IHttpClientFactory as private static but the recommended practice is, it should be private readonly, I'm not sure, why have you decorate with static, do you want the value of this variable would be changed only in the static constructor or not.

好的,根据您共享的代码片段,显然实现是可行的。然而,您可以在几个方面改进它。例如,您已经将IHttpClientFactory设置为私有静态,但推荐的做法是,它应该是私有只读的,我不确定,为什么要用静态来装饰,您是否希望此变量的值只在静态构造函数中更改。


Another important point is that, async call should be deal with Task and async Task is the natural approach because, if you use async void which has critical disadvantages and can restricted to your event handler also block your request pool. This is why async void is never encourage to use. You can get more details here.

另一个要点是,异步调用应该处理任务,而异步任务是自然的方法,因为如果您使用异步VALID,这有严重的缺点,并且可以限制到您的事件处理程序也会阻塞您的请求池。这就是为什么从来不鼓励使用Async Vole的原因。您可以在此处获取更多详细信息。


Finally, I fouond one inconsistency but its minor, however, I am little scrupulous here in this line, if you define HttpClient httpClient = null that means, you want this as nullable, so this should be as following:

最后,我发现了一个不一致之处,但它很小,然而,我在这一行中有点谨慎,如果您定义HttpClient httpClient=NULL,这意味着您希望它可以为空,因此应该如下所示:


public readonly HttpClient? httpClient = null;

So I left HttpClient? as nullable, thus the null assignment wouldn't yell at me.

所以我离开了HttpClient?因为可以为空,所以空赋值不会对我大喊大叫。


Therefore, I think, your whole implementation can be improved as following:

因此,我认为,您的整个实现可以改进如下:


public class MyController : Controller
{

private readonly IHttpClientFactory _httpClientFactory;
public readonly HttpClient? httpClient = null;

public MyController(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}

[HttpGet]
[Consumes("application/x-www-form-urlencoded")]
public async Task MyMethod1()
{
await httpClient.SendAsync();
}

}

Apart from above implementation, I would like to inform, depending on the requirement there are several ways for IHttpClientFactory you could consier.

除了上面的实现,我想告诉您,根据需求,IHttpClientFactory有几种方法可以考虑。


For example, CreateClient and Typedclients of IHttpClientFactory

例如,IHttpClientFactory的CreateClient和TypeClients


Note: Please refer to this official document, if you want to know more details about other consumption pattern.

注:如果您想了解更多有关其他消费模式的详细信息,请参阅本官方文件。


更多回答

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com