Created
March 27, 2019 12:58
-
-
Save jirolabo/f4f2ee7f96cf9351444d55d0d8629314 to your computer and use it in GitHub Desktop.
HttpClientFactory for console application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Net.Http; | |
using Microsoft.Extensions.DependencyInjection; | |
namespace ConsoleApp1 | |
{ | |
// Install-Package Microsoft.Extensions.DependencyInjection | |
// Install-Package Microsoft.Extensions.Http | |
class HttpClientCoreSapmle | |
{ | |
internal void Execute() | |
{ | |
var httpClient = new ServiceCollection() | |
.AddHttpClient() | |
.BuildServiceProvider() | |
.GetService<IHttpClientFactory>() | |
.CreateClient(); | |
var html = httpClient.GetStringAsync("https://twitter.com/ko_ro_chin").Result; | |
} | |
} | |
} |
Nice simple example. Works in test project too.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
+1