using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
using System.Collections.Generic;
static async Task Main(string[] args)
await Host.CreateDefaultBuilder(args).ConfigureServices((_, services) =>
services.AddTransient<ILibraryService, LibraryService>();
services.AddTransient(serviceProvider =>
return new Func<string, ILibraryApiClient>(baseUrl =>
var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(baseUrl);
httpClient.DefaultRequestHeaders.Add("Get", "application/json");
var logger = serviceProvider.GetRequiredService<ILogger<LibraryApiClient>>();
var client = new LibraryApiClient(baseUrl, httpClient, logger);
services.AddHostedService<ConsoleHostedService>();
}).UseContentRoot(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)).ConfigureLogging((_, builder) =>
builder.ClearProviders();
internal sealed class ConsoleHostedService : IHostedService
private readonly ILogger _logger;
private readonly IHostApplicationLifetime _appLifetime;
private readonly ILibraryService _libraryService;
public ConsoleHostedService(ILogger<ConsoleHostedService> logger, IHostApplicationLifetime appLifetime, ILibraryService libraryService)
_appLifetime = appLifetime;
_libraryService = libraryService;
public Task StartAsync(CancellationToken cancellationToken)
_logger.LogDebug($"Starting with arguments: {string.Join(" ", Environment.GetCommandLineArgs())}");
_appLifetime.ApplicationStarted.Register(() =>
await _libraryService.SearchAuthor("martha wells");
await _libraryService.DoOtherWork();
_logger.LogError(ex, "Unhandled exception!");
_appLifetime.StopApplication();
return Task.CompletedTask;
public Task StopAsync(CancellationToken cancellationToken)
return Task.CompletedTask;
public interface ILibraryService
Task SearchAuthor(string author);
public class LibraryService : ILibraryService
private readonly ILogger<LibraryService> _logger;
private readonly Func<string, ILibraryApiClient> _libraryApiClientFactory;
public LibraryService(ILogger<LibraryService> logger, Func<string, ILibraryApiClient> libraryApiClientFactory)
_libraryApiClientFactory = libraryApiClientFactory;
public async Task SearchAuthor(string author)
_logger.LogInformation("SearchAuthor - search using openlibrary api");
var booksResult = await _libraryApiClientFactory("http://openlibrary.org/").SearchAsync($"search.json?author={author}");
_logger.LogInformation(booksResult.Num_found.ToString());
public async Task DoOtherWork()
_logger.LogInformation("DoOtherWork");
public interface ILibraryApiClient
Task<BooksResult> SearchAsync(string searchTerm);
public class LibraryApiClient : ILibraryApiClient
private readonly HttpClient _httpClient;
private readonly ILogger<LibraryApiClient> _logger;
public LibraryApiClient(string baseUrl,
ILogger<LibraryApiClient> logger)
_httpClient = httpClient;
public async Task<BooksResult> SearchAsync(string searchTerm)
var response = await _httpClient.GetAsync(searchTerm);
var responseContent = await response.Content.ReadAsStringAsync();
_logger.LogInformation($"Result: {responseContent}");
var result = JsonConvert.DeserializeObject<BooksResult>(responseContent);
public int Start {get;set;}
public int Num_found {get;set;}
public List<Book> Docs {get;set;}