using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Net.Http.Json;
using System.Threading.Tasks;
public static async Task Main()
IServiceCollection services = new ServiceCollection();
services.AddLogging(d => d.AddConsole());
services.AddHttpClient<ICustomerSupportClient, CustomerSupportClient>(d =>
d.BaseAddress = new Uri("https://f6b15272-4aef-4b16-9582-71f6074bb4ed.mock.pstmn.io");
services.AddSingleton<Program>();
services.AddScoped<ICustomerSupportRepository, CustomerSupportRepository>();
IServiceProvider serviceProvider = services.BuildServiceProvider();
ILogger logger = serviceProvider.GetService<ILogger<Program>>();
ICustomerSupportClient client = serviceProvider.GetService<ICustomerSupportClient>();
logger.LogInformation("Demonstrating that server is live and actively returning results.");
IEnumerable<Customer> customers = await client.GetAllCustomersAsync();
foreach (var customer in customers)
logger.LogInformation("Id: {CustomerId}, Name: {Name}", customer.Id, customer.Name);
Program app = serviceProvider.GetRequiredService<Program>();
logger.LogError(ex, "Application failed.");
private readonly ILogger _logger;
private readonly ICustomerSupportRepository _customerSupportRepository;
public Program(ILogger<Program> logger, ICustomerSupportRepository customerSupportRepository)
_customerSupportRepository = customerSupportRepository;
_logger.LogInformation("Running application tasks");
internal interface ICustomerSupportRepository
internal class CustomerSupportRepository : ICustomerSupportRepository
private readonly ICustomerSupportClient _customerSupportClient;
public CustomerSupportRepository(ICustomerSupportClient customerSupportClient)
_customerSupportClient = customerSupportClient;
public int? Id { get; set; }
public string Name { get; set; }
public class SupportTicket
public int CustomerId { get; set; }
public int CreatedByUserId { get; set; }
public DateTime CreatedDate { get; set; }
public int Severity { get; set; }
public string Description { get; set; }
public string Status { get; set; }
public class SupportComment
public int UserId { get; set; }
public DateTime CreatedDate { get; set; }
public string Comment { get; set; }
internal interface ICustomerSupportClient
Task<IEnumerable<Customer>> GetAllCustomersAsync();
Task<IEnumerable<SupportTicket>> GetAllTicketsAsync();
internal class CustomerSupportClient : ICustomerSupportClient
private readonly HttpClient _httpClient;
public CustomerSupportClient(HttpClient httpClient)
_httpClient = httpClient;
public async Task<IEnumerable<Customer>> GetAllCustomersAsync()
return await _httpClient.GetFromJsonAsync<List<Customer>>("support/customers");
public async Task<IEnumerable<SupportTicket>> GetAllTicketsAsync()
return await _httpClient.GetFromJsonAsync<List<SupportTicket>>("support/tickets");