Пример #1
0
        public static async Task MessageReceiver(
            [EventHubTrigger(c_eventHubName, Connection = c_eventHubConnectionString)] EventData message,
            [Table(c_tableName)] CloudTable cloudTable,
            [SignalR(HubName = c_eventHubName, ConnectionStringSetting = c_signalRConnectionString)] IAsyncCollector <SignalRMessage> signalRMessages,
            ILogger log)
        {
            try
            {
                log.LogInformation($"Incoming request to {nameof(MessageReceiver)}");

                var counterProvider = new CounterProvider();

                // get counter
                var counter = await counterProvider.GetCounter(cloudTable, "counter");

                // update counter value
                counter.Count++;

                log.LogInformation($"New counter value {counter.Count}");

                // write counter to table
                var updateTask = counterProvider.UpdateCounter(cloudTable, counter);

                // update signal r
                var sendMessageTask = SendSignlRMessage(signalRMessages, counter.ToDTO());

                await Task.WhenAll(updateTask, sendMessageTask);
            }
            catch (Exception ex)
            {
                log.LogError(ex, $"Exception in {nameof(MessageReceiver)}: {ex.Message}");
            }
        }
Пример #2
0
        public static async Task <HttpResponseMessage> UpdateCounter(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequestMessage req,
            [Table(c_tableName)] CloudTable cloudTable,
            [SignalR(HubName = c_eventHubName, ConnectionStringSetting = c_signalRConnectionString)] IAsyncCollector <SignalRMessage> signalRMessages,
            ILogger log)
        {
            try
            {
                log.LogInformation($"Incoming request to {nameof(UpdateCounter)}");

                var dto = await ExtractContent <CounterDTO>(req);

                var counter = (Counter)dto;

                var counterProvider = new CounterProvider();

                // write counter to table
                var updateTask = counterProvider.UpdateCounter(cloudTable, counter);

                // update signal r
                var sendMessageTask = SendSignlRMessage(signalRMessages, counter.ToDTO());

                await Task.WhenAll(updateTask, sendMessageTask);
            }
            catch (Exception ex)
            {
                log.LogError(ex, $"Exception in {nameof(UpdateCounter)}: {ex.Message}");

                return(req.CreateResponse(HttpStatusCode.InternalServerError));
            }

            return(req.CreateResponse(HttpStatusCode.OK));
        }
Пример #3
0
        public static async Task <HttpResponseMessage> GetCounter(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequestMessage req,
            [Table(c_tableName)] CloudTable cloudTable,
            ILogger log)
        {
            try
            {
                log.LogInformation($"Incoming request to {nameof(GetCounter)}");

                var counterProvider = new CounterProvider();

                var id = req.RequestUri.ParseQueryString()["id"];
                // write counter to table
                var counter = await counterProvider.GetCounter(cloudTable, id);

                return(req.CreateResponse(HttpStatusCode.OK, counter.ToDTO()));
            }
            catch (Exception ex)
            {
                log.LogError(ex, $"Exception in {nameof(GetCounter)}: {ex.Message}");

                return(req.CreateResponse(HttpStatusCode.InternalServerError));
            }
        }