本文介绍了使用 .NET 4.6.2 中的 HttpClientFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .NET 4.6.2 控制台应用程序(使用 Simple Injector).我需要调用 HTTP 服务.直接使用 HttpClient 遇到问题后,我正在尝试使用 HttpClientFactory ( https://github.com/aspnet/HttpClientFactory ) 代替.

I have a .NET 4.6.2 console application (using Simple Injector). I need to make calls to an HTTP service. Having run into issues using HttpClient directly, I'm trying to use HttpClientFactory ( https://github.com/aspnet/HttpClientFactory ) instead.

项目/库是 .NET Standard 2.0 所以它应该??在 .NET 4.6.2 中工作,但它使用 IServiceCollection 之类的东西,它仅在 Core 中使用.

The project/library is .NET Standard 2.0 so it should?? work in .NET 4.6.2, but it uses stuff like IServiceCollection, which is in Core only.

所以我的问题是我可以在非核心应用程序中使用 HttpClientFactory.

So my question is can I use HttpClientFactory in a non-Core application.

推荐答案

所有 Microsoft.Extensions.* 包都面向 .NET Standard 2.0.这意味着,如果您将适当的包添加到您的应用程序中,您可以使用依赖注入、配置、日志记录 HttpClientFactory.

All of the Microsoft.Extensions.* packages target .NET Standard 2.0. That means that you can use Dependency Injection, Configuration, Logging and HttpClientFactory if you add the appropriate package to your application.

您需要添加 Microsoft.Extensions.Http 才能使用 HttpClientFactory 和Microsoft.Extensions.Http.Polly 如果您想将其与波莉

You need to add Microsoft.Extensions.Http to use HttpClientFactory and Microsoft.Extensions.Http.Polly if you want to use it with Polly

要配置 HttpClientFactory,您必须添加 Microsoft.Extensions.DependencyInjection 到您的主要"项目.Microsoft.Extensions.Http 包仅依赖于 Microsoft.Extensions.DependencyInjection.Abstractions 包含接口,而不是 DI 提供程序本身.

To configure HttpClientFactory you'll have to add Microsoft.Extensions.DependencyInjection to your "main" project. Microsoft.Extensions.Http package only depends on Microsoft.Extensions.DependencyInjection.Abstractions which contains the interfaces, not the DI provider itself.

这篇关于使用 .NET 4.6.2 中的 HttpClientFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 00:05