using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Threading.Tasks;
private static IServiceProvider ConfigureServices()
IServiceCollection services = new ServiceCollection();
services.AddLogging(d => d.AddConsole());
services.AddSingleton<Program>();
services.AddScoped<ICustomerSupportHttpClient, CustomerSupportHttpClient>();
services.Configure<JsonSerializerOptions>(options => {
options.AllowTrailingCommas = true;
options.PropertyNameCaseInsensitive = true;
services.AddScoped<ICustomerSupportService, CustomerSupportService>();
return services.BuildServiceProvider();
public static async Task Main()
IServiceProvider services = ConfigureServices();
ILogger logger = services.GetService<ILogger<Program>>();
ICustomerSupportHttpClient client = services.GetService<ICustomerSupportHttpClient>();
logger.LogInformation("This is just demonstrating that server is live and actively returning results.");
IEnumerable<Customer> customers = await client.GetAllCustomersAsync();
foreach (Customer customer in customers)
logger.LogInformation("Id: {CustomerId}, Name: {Name}", customer.Id, customer.Name);
Program app = services.GetRequiredService<Program>();
logger.LogError(ex, "Application failed.");
private readonly ILogger _logger;
private readonly ICustomerSupportService _customerSupportRepository;
public Program(ILogger<Program> logger, ICustomerSupportService customerSupportRepository)
_customerSupportRepository = customerSupportRepository;
_logger.LogInformation("Running application tasks");
_logger.LogInformation("Done");
return Task.CompletedTask;
internal interface ICustomerSupportService
internal class CustomerSupportService : ICustomerSupportService
private readonly ICustomerSupportHttpClient _customerSupportClient;
public CustomerSupportService(ICustomerSupportHttpClient customerSupportClient)
_customerSupportClient = customerSupportClient;
public int? Id { get; set; }
public string Name { get; set; }
public class SupportTicket
public int? Id { get; set; }
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 ICustomerSupportHttpClient
Task<IEnumerable<Customer>> GetAllCustomersAsync();
Task<IEnumerable<SupportTicket>> GetAllTicketsAsync();
internal class CustomerSupportHttpClient : ICustomerSupportHttpClient
private const string BASE_URL = "https://f6b15272-4aef-4b16-9582-71f6074bb4ed.mock.pstmn.io";
private readonly HttpClient _httpClient;
private readonly JsonSerializerOptions _jsonOptions;
public CustomerSupportHttpClient(IOptions<JsonSerializerOptions> jsonOptions)
Uri baseUri = new Uri(BASE_URL);
_httpClient = new HttpClient();
_httpClient.BaseAddress = baseUri;
_jsonOptions = jsonOptions.Value;
public async Task<IEnumerable<Customer>> GetAllCustomersAsync()
using HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "support/customers");
using HttpResponseMessage response = await _httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
using Stream responseBody = await response.Content.ReadAsStreamAsync();
return JsonSerializer.Deserialize<List<Customer>>(responseBody, _jsonOptions);
public async Task<IEnumerable<SupportTicket>> GetAllTicketsAsync()
using HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "support/tickets");
using HttpResponseMessage response = await _httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
using Stream responseBody = await response.Content.ReadAsStreamAsync();
return JsonSerializer.Deserialize<List<SupportTicket>>(responseBody, _jsonOptions);