IHttpClientFactory在.NET Framework 4.7.2中没有依赖注入功能。

huangapple go评论65阅读模式
英文:

IHttpClientFactory in .NET Framework 4.7.2 without Dependency Injection

问题

我计划在我的ASP.NET Framework 4.7.2 Webforms项目中使用Microsoft.Extensions.Http包。由于.NET Framework中没有内置的DI容器,我没有使用DI包。根据这个回答,我不确定最后一行的内容 -

Microsoft.Extensions.Http仅提供了HttpClientFactory,而不是新的优化HttpClient。这仅适用于.NET Core 2.1

在我的Framework项目中,我是否可以实现IHttpClientFactory而不使用DI,使用单例方法来解决直接使用HttpClient时出现的2个问题 - 套接字耗尽和DNS解析?根据上面的评论,是否还需要执行其他操作?

英文:

I am planning to use Microsoft.Extensions.Http package in my ASP.NET Framework 4.7.2 Webforms project. Since there is no built-in DI Container in .NET Framework, I am not using the DI packages. Based on this answer, I am not sure about the last line -

> Microsoft.Extensions.Http provides the HttpClientFactory only, not the
> new optimized HttpClient. This is only available in .NET Core 2.1

Can I implement IHttpClientFactory without DI and using singleton method in my Framework project and get rid of the 2 problems of using HttpClient directly - Socket Exhaustion and DNS resolution? Is there something else that needs to be done based on the above comment

答案1

得分: 8

抱歉,以下是翻译好的部分:

"Unfortunately, the use of the HttpClientFactory is tightly integrated with the DI framework. Fortunately, creating a new IHttpClientFactory without making use of the full DI infrastructure can be done in a few lines:"

"不幸的是,HttpClientFactory 的使用与 DI 框架紧密集成。幸运的是,可以在几行代码内创建一个新的 IHttpClientFactory,而无需使用完整的 DI 基础设施:"

"With the code above you create new new service provider (which is the MS.DI Container) that just contains the registrations for the HTTP client package, which includes a registration for IHttpClientFactory, and the IHttpClientFactory is directly pulled from the container. The factory is stored in a variable, while the container itself is no longer used."

"使用上述代码,您创建了一个新的服务提供程序(即 MS.DI 容器),其中只包含 HTTP 客户端包的注册信息,其中包括对 IHttpClientFactory 的注册,并且直接从容器中获取了 IHttpClientFactory。工厂被存储在一个变量中,而容器本身不再使用。"

"A full working Console application would like like this:"

"一个完整的控制台应用程序应该如下所示:"

"Best is to cache the IHttpClientFactory for the lifetime of your application and not recreate it on the fly."

"最好是在应用程序的生命周期内缓存 IHttpClientFactory,而不是动态重新创建它。"

英文:

Unfortunately, the use of the HttpClientFactory is tightly integrated with the DI framework. Fortunately, creating a new IHttpClientFactory without making use of the full DI infrastructure can be done in a few lines:

IHttpClientFactory factory = new ServiceCollection()
    .AddHttpClient()
    .BuildServiceProvider()
    .GetRequiredService<IHttpClientFactory>();

With the code above you create new new service provider (which is the MS.DI Container) that just contains the registrations for the HTTP client package, which includes a registration for IHttpClientFactory, and the IHttpClientFactory is directly pulled from the container. The factory is stored in a variable, while the container itself is no longer used.

A full working Console application would like like this:

// This requires the 'Microsoft.Extensions.Http` package to be installed
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Net.Http;
using System.Threading.Tasks;

internal class Program
{
    static async Task Main(string[] args)
    {
        IHttpClientFactory factory = new ServiceCollection()
            .AddHttpClient()
            .BuildServiceProvider()
            .GetRequiredService<IHttpClientFactory>();

        HttpClient client = factory.CreateClient();

        string html = await client.GetStringAsync(
            "https://blogs.cuttingedge.it/steven/p/commands");

        Console.WriteLine(html);

        Console.ReadLine();
    }
}

Best is to cache the IHttpClientFactory for the lifetime of your application and not recreate it on the fly.

huangapple
  • 本文由 发表于 2023年1月9日 18:20:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定