-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathProgram.cs
More file actions
42 lines (38 loc) · 1.35 KB
/
Program.cs
File metadata and controls
42 lines (38 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace HttpClientConsoleSample
{
class Program
{
static async Task Main(string[] args)
{
var builder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddHttpClient();
services.AddTransient<IBlog, Blog>();
}).UseConsoleLifetime();
var host = builder.Build();
using (var serviceScope = host.Services.CreateScope())
{
var services = serviceScope.ServiceProvider;
try
{
var blogService = services.GetRequiredService<IBlog>();
var article = await blogService.GetArticle("2020/02/aspnet-core-provider-mask-api.html");
// output first 1000 words.
Console.WriteLine(article.Substring(0, 1000));
Console.Read();
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred.");
}
}
}
}
}