public override Task <ObtainTokenReply> Obtain(ObtainTokenRequest request, ServerCallContext context) { try { var token = _tokenGen.Obtain(request.Client); //Console.WriteLine($"Generated Token:- Id:{token.Id} IssuedOn:{token.IssuedOn.ToLongTimeString()} Client:{request.Client}"); _logger?.LogInformation($"Generated Token:- Id:{token?.Id} IssuedOn:{token?.IssuedOn.ToLongTimeString()} Client:{request.Client}"); return(Task.FromResult(new ObtainTokenReply { Id = token?.Id, Issuedon = Timestamp.FromDateTime(token?.IssuedOn.ToUniversalTime() ?? DateTime.MinValue) })); } catch (Exception ex) { _logger?.LogError(ex, $"Exception obtaining token client:{request.Client}"); throw; } }
private async Task <Token[]> CallExternalApiInParallelAsync() { var listOfTokenTasks = new List <Task <Token> >(); for (var i = 0; i < nmberOfCalls; i++) { listOfTokenTasks.Add(Task.Run(async() => { Token token = _tokenService.Obtain(MyClientName); Console.WriteLine($"Received Token {token.Id} for client {MyClientName}"); await Task.Delay(simulatedApiDurationInMillSecs).ConfigureAwait(false);//emulate calling a WebApi. return(token); })); } var result = await Task.WhenAll(listOfTokenTasks).ConfigureAwait(false); return(result); }