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

查看:307
本文介绍了我可以在不是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天全站免登陆