private async Task <HttpResponseMessage> FallbackAsync(HttpRequestMessage request, string utterance, LuisRecognizerOptionsV3 recognizer, CancellationToken cancellationToken)
        {
            var response = ResponsePath(utterance, recognizer);

            if (File.Exists(response))
            {
                var luisResult = File.ReadAllText(response);
                return(new HttpResponseMessage
                {
                    Content = new StringContent(luisResult, Encoding.UTF8, "application/json"),
                });
            }
            else
            {
                HttpResponseMessage result = null;
                using (var client = new HttpClient())
                    using (var clonedRequest = await MockedHttpClientHandler.CloneHttpRequestMessageAsync(request).ConfigureAwait(false))
                    {
                        result = await client.SendAsync(clonedRequest, cancellationToken).ConfigureAwait(false);
                    }

                var luisResult = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

                File.WriteAllText(response, luisResult);
                return(result);
            }
        }
        /// <inheritdoc/>
        public override async Task <RecognizerResult> RecognizeAsync(DialogContext dialogContext, Activity activity, CancellationToken cancellationToken = default, Dictionary <string, string> telemetryProperties = null, Dictionary <string, double> telemetryMetrics = null)
        {
            HttpClientHandler newHandler = null, oldHandler = _recognizer.HttpClient;

            // Used for ResponsePath
            var recognizer = _recognizer.RecognizerOptions(dialogContext);

            foreach (var middware in dialogContext.Context.Adapter.MiddlewareSet)
            {
                if (middware is TelemetryLoggerMiddleware telemetryMiddleware)
                {
                    _recognizer.TelemetryClient = telemetryMiddleware.TelemetryClient;
                }
            }

            recognizer.IncludeAPIResults = true;

            var middleware = dialogContext.Context.TurnState.Get <MockHttpRequestMiddleware>();

            if (middleware == null)
            {
#pragma warning disable CA2000 // Dispose objects before losing scope
                var mockHandler = new MockHttpMessageHandler();
#pragma warning restore CA2000 // Dispose objects before losing scope
                mockHandler.Fallback.Respond((request) => FallbackAsync(request, activity.Text, recognizer, cancellationToken).GetAwaiter().GetResult());
                newHandler = new MockedHttpClientHandler(mockHandler);
            }
            else
            {
                middleware.SetFallback((request) => FallbackAsync(request, activity.Text, recognizer, cancellationToken).GetAwaiter().GetResult());
                newHandler = new MockedHttpClientHandler(dialogContext.Context.TurnState.Get <HttpMessageHandler>());
            }

            _recognizer.HttpClient = newHandler;
            var result = await _recognizer.RecognizeAsync(dialogContext, activity, cancellationToken, telemetryProperties, telemetryMetrics).ConfigureAwait(false);

            _recognizer.HttpClient = oldHandler;
            if (middleware != null)
            {
                middleware.SetFallback(null);
            }

            return(result);
        }
        private HttpClientHandler GetMockedClient(string utterance, LuisRecognizerOptionsV3 recognizer)
        {
            HttpClientHandler client = null;

            if (utterance != null)
            {
                var response = ResponsePath(utterance, recognizer);
                if (File.Exists(response))
                {
                    var handler = new MockHttpMessageHandler();
                    handler
                    .When(recognizer.Application.Endpoint + "*")
                    .WithPartialContent(utterance)
                    .Respond("application/json", File.OpenRead(response));
                    client = new MockedHttpClientHandler(handler.ToHttpClient());
                }
            }

            return(client);
        }
예제 #4
0
        private HttpClientHandler GetMockedClient(string utterance, LuisRecognizerOptionsV3 recognizer)
        {
            HttpClientHandler client = null;

            if (utterance != null)
            {
                var response = ResponsePath(utterance, recognizer);
                if (File.Exists(response))
                {
#pragma warning disable CA2000 // Dispose objects before losing scope (ownership of handler is passed to MockedHttpClientHandler, that object should dispose it)
                    var handler = new MockHttpMessageHandler();
#pragma warning restore CA2000 // Dispose objects before losing scope
                    handler
                    .When(recognizer.Application.Endpoint + "*")
                    .WithPartialContent(utterance)
#pragma warning disable CA2000 // Dispose objects before losing scope (ownership of the File.OpenRead() stream is passed to MockedHttpClientHandler, that object should dispose it)
                    .Respond("application/json", File.OpenRead(response));
#pragma warning restore CA2000 // Dispose objects before losing scope
                    client = new MockedHttpClientHandler(handler.ToHttpClient());
                }
            }

            return(client);
        }