我可以在非 ASP.NET Core 的 .NET.core 应用程序中使用 HttpClientFactory 吗? [英] Can I use HttpClientFactory in a .NET.core app which is not ASP.NET Core?

查看:31
本文介绍了我可以在非 ASP.NET Core 的 .NET.core 应用程序中使用 HttpClientFactory 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读流行的博客文章 https://www.stevejgordon.co.uk/introduction-to-httpclientfactory-aspnetcore 使用 HttpClientFactory

I have read the popular blog post https://www.stevejgordon.co.uk/introduction-to-httpclientfactory-aspnetcore on using HttpClientFactory

引用它

ASP.NET Core 2.1 中新增了 HttpClientFactory 功能,有助于解决开发人员在使用 HttpClient 实例从其应用程序发出外部 Web 请求时可能遇到的一些常见问题.

A new HttpClientFactory feature is coming in ASP.NET Core 2.1 which helps to solve some common problems that developers may run into when using HttpClient instances to make external web requests from their applications.

所有示例都显示了在 asp.net 应用程序的启动类中将其连接起来,例如

All the examples show wiring it up in startup class of asp.net application e.g.

public void ConfigureServices(IServiceCollection services)
{
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.AddHttpClient();
} 

我的问题是您可以在 ASP.NET 核心之外使用吗?如果有,有例子吗

My question is can you use outside of ASP.NET core? And if so are there examples

我本以为很多非 Web 应用程序(.net 核心应用程序)需要进行 Web 调用,那么为什么这不是 .net core api 的一部分,而是放入 asp.net core api 中

I would have thought lots of non web applications(.net core apps) need to make web calls, so why was this not part of .net core api instead of putting into asp.net core api

推荐答案

感谢回复.

因此可以在控制台应用程序中使用.

So it is possible to use in console app.

有几种方法可以做到这一点,具体取决于您想走的路.这里有 2 个:

There are a few ways to do this, depending on what way you want to go. Here are 2:

  1. 直接添加到 ServiceCollection 例如services.AddHttpClient()

  1. Directly add to ServiceCollection e.g. services.AddHttpClient()

使用通用主机 例如在 .ConfigureServices() 方法中添加 httpclientFactory

Use Generic host e.g. Add httpclientFactory in .ConfigureServices() method

见这里在控制台应用程序中使用的博客文章

这篇关于我可以在非 ASP.NET Core 的 .NET.core 应用程序中使用 HttpClientFactory 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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