using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using System.Threading.Tasks;
public static async Task Main()
for (int i = 0; i < 10; i++)
Console.WriteLine("I=" + i);
static async Task MainAsync()
Console.WriteLine("Building service provider");
IServiceProvider serviceProvider = Startup.BuildServiceProvider();
Console.WriteLine("Starting service");
await serviceProvider.GetService<App>().Run();
Console.WriteLine("Ending service");
Console.WriteLine($"Error running service {ex.Message}");
private readonly IMbcpServiceService _mbcpService;
public App(IMbcpServiceService mbcpService)
_mbcpService = mbcpService;
var content = await _mbcpService.GetMbcpParticularesHomePageAsync();
Console.WriteLine(content);
public interface IMbcpServiceService
Task<string> GetMbcpParticularesHomePageAsync();
public class MbcpService : IMbcpServiceService
private readonly string _mbcpHomepageUrl;
private const string _mbcpParticularesHomePageContentCacheKey = "MbcpParticularesHomePageContent";
private readonly IMemoryCache _memoryCache;
private readonly IHttpClientFactory _httpClientFactory;
public MbcpService(IConfigurationRoot configuration, IMemoryCache memoryCache, IHttpClientFactory httpClientFactory)
_mbcpHomepageUrl = configuration["MbcpHomepageUrl"];
_memoryCache = memoryCache;
_httpClientFactory = httpClientFactory;
public async Task<string> GetMbcpParticularesHomePageAsync()
if (_memoryCache.TryGetValue(_mbcpParticularesHomePageContentCacheKey, out string content))
var client = _httpClientFactory.CreateClient();
client.BaseAddress = new Uri(_mbcpHomepageUrl);
using var response = await client.GetAsync("/pt/particulares/Pages/Welcome.aspx");
response.EnsureSuccessStatusCode();
content = await response.Content.ReadAsStringAsync();
MemoryCacheEntryOptions cacheEntryOptions = GetMemoryCacheEntryOptions();
_memoryCache.Set(_mbcpParticularesHomePageContentCacheKey, content, cacheEntryOptions);
private static MemoryCacheEntryOptions GetMemoryCacheEntryOptions()
return new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromMilliseconds(500))
.SetPriority(CacheItemPriority.Normal)
public Uri[] MbcpHomepageUrl { get; set; }
private static IConfigurationRoot _configuration;
private static ServiceCollection _serviceCollection;
private static ServiceProvider _serviceProvider;
public static ServiceProvider BuildServiceProvider()
#region Configure Services
if (_serviceCollection == null)
_serviceCollection = new ServiceCollection();
var configurationBuilder = new ConfigurationBuilder();
var inMemorySettings = new Dictionary<string, string>
{"MbcpHomepageUrl", "http://ind.millenniumbcp.pt"}
_configuration = new ConfigurationBuilder()
.AddInMemoryCollection(inMemorySettings)
_serviceCollection.AddSingleton<IConfigurationRoot>(_configuration);
_serviceCollection.AddMemoryCache();
_serviceCollection.AddHttpClient();
_serviceCollection.AddSingleton<App>();
_serviceCollection.AddSingleton<IMbcpServiceService, MbcpService>();
#region BuildServiceProvider
_serviceProvider = _serviceCollection.BuildServiceProvider();