/// <summary>
        /// Initializes a new instance of the <see cref="LuisRecognizer"/> class.
        /// </summary>
        /// <param name="application">The LUIS application to use to recognize text.</param>
        /// <param name="predictionOptions">(Optional) The LUIS prediction options to use.</param>
        /// <param name="includeApiResults">(Optional) TRUE to include raw LUIS API response.</param>
        /// <param name="clientHandler">(Optional) Custom handler for LUIS API calls to allow mocking.</param>
        public LuisRecognizer(LuisApplication application, LuisPredictionOptions predictionOptions = null, bool includeApiResults = false, HttpClientHandler clientHandler = null)
        {
            _application       = application ?? throw new ArgumentNullException(nameof(application));
            _options           = predictionOptions ?? new LuisPredictionOptions();
            _includeApiResults = includeApiResults;

            TelemetryClient        = _options.TelemetryClient;
            LogPersonalInformation = _options.LogPersonalInformation;

            var credentials       = new ApiKeyServiceClientCredentials(application.EndpointKey);
            var delegatingHandler = new LuisDelegatingHandler();
            var httpClientHandler = clientHandler ?? CreateRootHandler();
            var currentHandler    = CreateHttpHandlerPipeline(httpClientHandler, delegatingHandler);

            DefaultHttpClient = new HttpClient(currentHandler, false)
            {
                Timeout = TimeSpan.FromMilliseconds(_options.Timeout),
            };

            // assign DefaultHttpClient to _httpClient to expose Timeout in unit tests
            // and keep HttpClient usage as a singleton
            _httpClient = DefaultHttpClient;

            _runtime = new LUISRuntimeClient(credentials, _httpClient, false)
            {
                Endpoint = application.Endpoint,
            };
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="LuisRecognizer"/> class.
        /// </summary>
        /// <param name="recognizerOptions"> The LUIS recognizer version options.</param>
        /// <param name="clientHandler">(Optional) Custom handler for LUIS API calls to allow mocking.</param>
        public LuisRecognizer(LuisRecognizerOptions recognizerOptions, HttpClientHandler clientHandler = null)
        {
            _luisRecognizerOptions = recognizerOptions;

            TelemetryClient        = recognizerOptions.TelemetryClient;
            LogPersonalInformation = recognizerOptions.LogPersonalInformation;

            var delegatingHandler = new LuisDelegatingHandler();
            var httpClientHandler = clientHandler ?? CreateRootHandler();
            var currentHandler    = CreateHttpHandlerPipeline(httpClientHandler, delegatingHandler);

            DefaultHttpClient = new HttpClient(currentHandler, false)
            {
                Timeout = TimeSpan.FromMilliseconds(recognizerOptions.Timeout),
            };
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LuisRecognizer"/> class.
        /// </summary>
        /// <param name="application">The LUIS application to use to recognize text.</param>
        /// <param name="predictionOptions">(Optional) The LUIS prediction options to use.</param>
        /// <param name="includeApiResults">(Optional) TRUE to include raw LUIS API response.</param>
        /// <param name="clientHandler">(Optional) Custom handler for LUIS API calls to allow mocking.</param>
        public LuisRecognizer(LuisApplication application, LuisPredictionOptions predictionOptions = null, bool includeApiResults = false, HttpClientHandler clientHandler = null)
        {
            _application       = application ?? throw new ArgumentNullException(nameof(application));
            _options           = predictionOptions ?? new LuisPredictionOptions();
            _includeApiResults = includeApiResults;

            var credentials       = new ApiKeyServiceClientCredentials(application.EndpointKey);
            var delegatingHandler = new LuisDelegatingHandler();

            // LUISRuntimeClient requires that we explicitly bind to the appropriate constructor.
            _runtime = clientHandler == null
                    ?
                       new LUISRuntimeClient(credentials, delegatingHandler)
                    :
                       new LUISRuntimeClient(credentials, clientHandler, delegatingHandler);

            _runtime.Endpoint = application.Endpoint;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="LuisRecognizer"/> class.
        /// </summary>
        /// <param name="application">The LUIS application to use to recognize text.</param>
        /// <param name="recognizerOptions">(Optional) Options for the created recognizer.</param>
        /// <param name="predictionOptions">(Optional) The default LUIS prediction options to use.</param>
        public LuisRecognizer(LuisApplication application, LuisRecognizerOptions recognizerOptions = null, LuisPredictionOptions predictionOptions = null)
        {
            recognizerOptions  = recognizerOptions ?? new LuisRecognizerOptions();
            _application       = application ?? throw new ArgumentNullException(nameof(application));
            _predictionOptions = predictionOptions ?? new LuisPredictionOptions();

            TelemetryClient        = recognizerOptions.TelemetryClient;
            LogPersonalInformation = recognizerOptions.LogPersonalInformation;

            var delegatingHandler = new LuisV2.LuisDelegatingHandler();
            var httpClientHandler = recognizerOptions.HttpClient ?? CreateRootHandler();
            var currentHandler    = CreateHttpHandlerPipeline(httpClientHandler, delegatingHandler);

            DefaultHttpClient = new HttpClient(currentHandler, false)
            {
                Timeout = recognizerOptions.Timeout,
            };

            DefaultHttpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _application.EndpointKey);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="LuisRecognizer"/> class.
        /// </summary>
        /// <param name="recognizerOptions"> The LUIS recognizer version options.</param>
        /// <param name="clientHandler">(Optional) Custom handler for LUIS API calls to allow mocking.</param>
        public LuisRecognizer(LuisRecognizerOptions recognizerOptions, HttpClientHandler clientHandler = null)
        {
            _luisRecognizerOptions = recognizerOptions;

            TelemetryClient        = recognizerOptions.TelemetryClient;
            LogPersonalInformation = recognizerOptions.LogPersonalInformation;

            var delegatingHandler = new LuisDelegatingHandler();
            var httpClientHandler = clientHandler ?? CreateRootHandler();

#pragma warning disable CA2000 // Dispose objects before losing scope (suppressing this warning, for now! we will address this once we implement HttpClientFactory in a future release)
            var currentHandler = CreateHttpHandlerPipeline(httpClientHandler, delegatingHandler);
#pragma warning restore CA2000 // Dispose objects before losing scope

            HttpClient = new HttpClient(currentHandler, false)
            {
                Timeout = TimeSpan.FromMilliseconds(recognizerOptions.Timeout),
            };

#pragma warning disable 618 // Reference to obsolete property, this is here only for backward compat and should be removed when DefaultHttpClient is removed.
            DefaultHttpClient = HttpClient;
#pragma warning restore 618
        }