Esempio n. 1
0
        public async Task <Client> SaveClientToDatabase([ActivityTrigger] Client client)
        {
            client.CreatedTime = DateTime.Now;

            dbContext.Clients.Add(client);
            await dbContext.SaveChangesAsync();

            return(client);
        }
        public async Task <IActionResult> Post([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log,
                                               [ServiceBus("%ServiceBusQueueName%", Connection = "ServiceBusConnectionString", EntityType = EntityType.Queue)] ICollector <string> messages)
        {
            try
            {
                //deserialize to Client object
                var content = await req.ReadAsStringAsync();

                var client = JsonConvert.DeserializeObject <Client>(content);

                client.CreatedTime = DateTime.Now;

                dbContext.Clients.Add(client);
                await dbContext.SaveChangesAsync();

                messages.Add(JsonConvert.SerializeObject(client));

                return(new OkObjectResult(client));
            }
            catch (Exception ex)
            {
                log.LogError(ex, "Error Registering client");
                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }
        }
        public async Task GenerateDocument(
            [ServiceBusTrigger("%ServiceBusQueueName%", Connection = "ServiceBusConnectionString")] string myQueueItem,
            ILogger log,
            [Blob("clients/{rand-guid}.txt", FileAccess.ReadWrite, Connection = "AzureStorageConnectionString")] CloudBlockBlob blob)
        {
            var client = JsonConvert.DeserializeObject <Client>(myQueueItem);
            var html   = await FetchHtml(client.Url);

            var bytes = Encoding.ASCII.GetBytes(html);

            //long running process
            await Task.Delay(1000 *new Random().Next(10, 30));

            await blob.UploadFromByteArrayAsync(bytes, 0, bytes.Length);

            client.DocumentUrl       = blob.Uri.ToString();
            client.DocumentGenerated = DateTime.Now;
            dbContext.Clients.Update(client);
            await dbContext.SaveChangesAsync();
        }