Exemplo n.º 1
0
        public static async Task <HttpResponseMessage> RunAsync(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "analyze")] HttpRequestMessage request,
            TraceWriter log,
            ExecutionContext context,
            CancellationToken cancellationToken)
        {
            using (IUnityContainer childContainer = Container.CreateChildContainer().WithTracer(log, true))
            {
                // Create a tracer for this run (that will also log to the specified TraceWriter)
                ITracer tracer = childContainer.Resolve <ITracer>();
                tracer.TraceInformation($"Analyze function request received with invocation Id {context.InvocationId}");
                tracer.AddCustomProperty("FunctionName", context.FunctionName);
                tracer.AddCustomProperty("InvocationId", context.InvocationId.ToString("N"));

                try
                {
                    // Trace app counters (before analysis)
                    tracer.TraceAppCounters();

                    // Read the request
                    SmartSignalRequest smartSignalRequest = await request.Content.ReadAsAsync <SmartSignalRequest>(cancellationToken);

                    tracer.AddCustomProperty("SignalId", smartSignalRequest.SignalId);
                    tracer.TraceInformation($"Analyze request received: {JsonConvert.SerializeObject(smartSignalRequest)}");

                    // Process the request
                    ISmartSignalRunner runner = childContainer.Resolve <ISmartSignalRunner>();
                    List <SmartSignalResultItemPresentation> resultPresentations = await runner.RunAsync(smartSignalRequest, cancellationToken);

                    tracer.TraceInformation($"Analyze completed, returning {resultPresentations.Count} results");

                    // Return the generated presentations
                    return(request.CreateResponse(HttpStatusCode.OK, resultPresentations));
                }
                catch (Exception e)
                {
                    // Handle the exception
                    TopLevelExceptionHandler.TraceUnhandledException(e, tracer, log);

                    // Return error status
                    return(request.CreateResponse(HttpStatusCode.InternalServerError, e.Message));
                }
                finally
                {
                    // Trace app counters (after analysis)
                    tracer.TraceAppCounters();
                }
            }
        }
        /// <summary>
        /// Run the signal, by delegating the call to the registered <see cref="ISmartSignalRunner"/>
        /// </summary>
        /// <param name="request">The signal request</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <returns>A <see cref="Task{TResult}"/>, returning the result items presentations generated by the signal</returns>
        private static async Task <List <SmartSignalResultItemPresentation> > RunSignalAsync(SmartSignalRequest request, CancellationToken cancellationToken)
        {
            ISmartSignalRunner smartSignalRunner = container.Resolve <ISmartSignalRunner>();

            return(await smartSignalRunner.RunAsync(request, cancellationToken));
        }