public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "history/{binId}")] HttpRequest request, [DurableClient] IDurableClient client, string binId, ILogger log) { try { log.LogInformation(new EventId(300), "{BinId}, {Message}", binId, $"A request to delete request history for bin '{binId}' has been received."); if (!RequestBinService.IsBinIdValid(binId, out var validationMessage)) { log.LogError(new EventId(391), "{BinId}, {Message}", binId, $"Invalid Bin Id '{binId}'."); return(new BadRequestObjectResult(validationMessage)); } var encodedBinId = RequestBinService.EncodeBinId(binId); //Send a one-way message to an entity (via an abstracted queue) using a proxy object for type-safe calls. await client.SignalEntityAsync <IRequestBin>(encodedBinId, x => x.Empty()); log.LogInformation(new EventId(310), "{BinId}, {Message}", binId, $"Request history for bin '{binId}' has been deleted."); return(new OkResult()); } catch (Exception ex) { log.LogError(new EventId(390), ex, "{BinId}", binId, $"Error occurred while trying to delete request history for bin: '{binId}'"); throw; } }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "put", "patch", "delete", "head", "options", "trace", Route = "{binId}")] HttpRequest request, [DurableClient] IDurableClient client, string binId, ILogger log) { try { log.LogInformation(new EventId(100), "{BinId}, {Message}", binId, $"Request received for bin '{binId}'."); if (!RequestBinService.IsBinIdValid(binId, out var validationMessage)) { log.LogError(new EventId(191), "{BinId}, {Message}", binId, $"Invalid Bin Id '{binId}'."); return(new BadRequestObjectResult(validationMessage)); } var requestDescription = await RequestBinService.GetRequestDescriptionAsync(request); var encodedBinId = RequestBinService.EncodeBinId(binId); //Send a one-way message to an entity (via an abstracted queue) using a proxy object for type-safe calls. await client.SignalEntityAsync <IRequestBin>(encodedBinId, x => x.Add(requestDescription)); log.LogInformation(new EventId(110), "{BinId}, {Message}", binId, $"Request for bin '{binId}' stored."); return(new OkResult()); } catch (Exception ex) { log.LogError(new EventId(190), ex, "{BinId}, {Message}", binId, $"Error occurred while trying to persist request into bin: '{binId}'."); throw; } }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "history/{binId}")] HttpRequest request, [DurableClient] IDurableClient client, string binId, ILogger log, ExecutionContext context) { try { log.LogInformation(new EventId(200), "{BinId}, {Message}", binId, $"A request to return request history for bin '{binId}' has been received."); if (!RequestBinService.IsBinIdValid(binId, out var validationMessage)) { log.LogError(new EventId(291), "{BinId}, {Message}", binId, $"Invalid Bin Id '{binId}'."); return(NewHtmlContentResult(HttpStatusCode.BadRequest, RequestBinRenderer.RenderToString(binId, "Invalid", null, validationMessage))); } var binUrl = $"http{(request.IsHttps ? "s" : "")}://{request.Host}{request.Path.ToString().Replace("/history", "")}"; var encodedBinId = RequestBinService.EncodeBinId(binId); var durableRequestBinId = new EntityId(nameof(RequestBin), encodedBinId); //Read the state of a Durable Entity var durableRequestBin = await client.ReadEntityStateAsync <RequestBin>(durableRequestBinId); var requestBinHistory = RequestBinRenderer.RenderToString(binId, binUrl, durableRequestBin.EntityState?.RequestHistoryItems); log.LogInformation(new EventId(210), "{BinId}, {Message}", binId, $"Request history for bin '{binId}' returned successfully."); return(NewHtmlContentResult(HttpStatusCode.OK, requestBinHistory)); } catch (Exception ex) { log.LogError(new EventId(290), ex, "{BinId}", binId, $"Error occurred trying to return the request history for bin: '{binId}'"); try { return(NewHtmlContentResult(HttpStatusCode.InternalServerError, RequestBinRenderer.RenderToString(binId, "", null, $"500 Internal Server Error. Execution Id: '{context.InvocationId.ToString()}'"))); } catch (Exception) { //In case the custom Html render didn't work, return a message without format. return(NewHtmlContentResult(HttpStatusCode.InternalServerError, $"500 Internal Server Error. Execution Id: '{context.InvocationId.ToString()}'")); } } }