public async Task <string> GetAliasAsync(string alias) { string result; _telemetryClient.GetMetric("MethodUsage", "Method").TrackValue(1, "GetAlias"); _logger.LogInformation("Fetching alias"); using (var sqlConnection = new SqlConnection(_configuration.GetString("Database.ConnectionString"))) { using (var command = new SqlCommand(GetAliasQuery, sqlConnection)) { command.Parameters.AddWithValue("alias", alias); await sqlConnection.OpenAsync(); result = (string)await command.ExecuteScalarAsync(); } } _logger.LogDebug("Recording usage"); var redisConnection = _redisDatabaseFactory.GetDatabase(); await Task.WhenAll( redisConnection.HashIncrementAsync(alias, "requestCount"), redisConnection.HashSetAsync(alias, "lastRequest", DateTime.UtcNow.ToString("O"))); return(result); }
public IDatabase GetDatabase() { // Note: locking is not like amazing, but still better than doing work in the constructor (especially work over the network) if (_multiplexer == null) { lock (_multiplexerLock) { if (_multiplexer == null) { var connectionString = _runtimeConfiguration.GetString("Redis.ConnectionString"); _multiplexer = ConnectionMultiplexer.Connect(connectionString); } } } return(_multiplexer.GetDatabase()); }
public async Task ExecuteAsync() { var baseUrl = _runtimeConfiguration.GetString("UrlShortner.BaseUrl"); var alias = Guid.NewGuid().ToString(); var url = "https://null.null/null"; await HttpClient.PostAsJsonAsync($"{baseUrl}/api/UrlShortner", new AddAliasProperties { Alias = alias, Url = url }); var tasks = Enumerable.Range(0, 5) .Select(x => HttpClient.GetAsync($"{baseUrl}/api/UrlShortner/?a={alias}")); try { await Task.WhenAll(tasks); } catch (Exception) { // I am redirecting to a non-existend host so this is expected } }
private SqlConnection CreateConnection() { var connectionString = _runtimeConfiguration.GetString("Database.ConnectionString"); return(new SqlConnection(connectionString)); }