除了.Net核心之外,如何在.net框架中实现IHttpClientFactory? [英] How do I implement IHttpClientFactory in .net framework apart from .Net core?

查看:253
本文介绍了除了.Net核心之外,如何在.net框架中实现IHttpClientFactory?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.Net核心中,我们可以使用IHttpClientFactory在运行时进行注入和使用.作为开发人员,我无需担心依赖关系解析.我只需要在服务集合中指定AddHttpClient()即可. 同样,如何在WPF应用程序.net Framework 4.6.1中使用IHttpClientFactory?由于没有可用于WPF应用程序的Service集合,因此无法获取解决依赖关系的方法.

In .Net core, we can use IHttpClientFactory to inject and use during runtime. As a developer I no need to worry about the dependency resolution. I need to just specify AddHttpClient() in service collection. Likewise, how can I use IHttpClientFactory in WPF application, .net framework 4.6.1? As there is no Service collection available for WPF application, not getting how to resolve the dependency.

推荐答案

您可以添加 Microsoft.Extensions.Http NuGet包到任何.NET Standard 2.0兼容项目.其中包括针对.NET 4.6.1的项目

You can add the Microsoft.Extensions.Http NuGet package to any .NET Standard 2.0-compliant project. That includes projects that target .NET 4.6.1

所有Microsoft.Extensions.*软件包都是.NET Standard 2.0软件包,这意味着您可以使用与.NET Core相同的配置,依赖项注入,日志记录服务.

All of the Microsoft.Extensions.* packages are .NET Standard 2.0 packages, which means you can use the same Configuration, Dependency Injection, Logging services as .NET Core.

Microsoft.Extensions.Http仅提供HttpClientFactory,而不提供新的优化HttpClient.仅在.NET Core 2.1中可用

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

这篇关于除了.Net核心之外,如何在.net框架中实现IHttpClientFactory?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆