Бесконечная загрузка при вызове QnAMaker с использованием WebMethod C # - PullRequest
0 голосов
/ 10 июня 2019

Когда я пытался вызвать веб-метод "QnAMakerQuestionsAnswers", он будет иметь только бесконечную загрузку и не будет показывать результат. Я пробовал это в консольном приложении, и оно отлично работает, но не для WebMethod. Я не уверен, какую часть моего кода мне нужно изменить, но это немного расстраивает. Пожалуйста помоги!! Код ниже:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace ChatbotWebService
{
    /// <summary>
    /// Summary description for ChatbotWebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ChatbotWebService : System.Web.Services.WebService
    {
        // NOTE: Replace this with a valid host name.
        static string host = "<Host Name>";

        // NOTE: Replace this with a valid endpoint key.
        // This is not your subscription key.
        // To get your endpoint keys, call the GET /endpointkeys method.
        static string endpoint_key = "<End Point Key>";

        // NOTE: Replace this with a valid knowledge base ID.
        // Make sure you have published the knowledge base with the
        // POST /knowledgebases/{knowledge base ID} method.
        static string kb = "<KB ID>";

        static string service = "/qnamaker";
        static string method = "/knowledgebases/" + kb + "/generateAnswer/";

        static string question = @"
        {
            'question': 'Who are you?'
        }
        ";

        async static Task<string> Post(string uri, string body)
        {
            using (var client = new HttpClient())
            using (var request = new HttpRequestMessage())
            {
                request.Method = HttpMethod.Post;
                request.RequestUri = new Uri(uri);
                request.Content = new StringContent(body, Encoding.UTF8, "application/json");
                request.Headers.Add("Authorization", "EndpointKey " + endpoint_key);
                //SUPER IMPORTANT LINE. PLEASE DO IT FOR .NET Framework v4.5.
                //Explanation at https://blogs.perficient.com/2016/04/28/tsl-1-2-and-net-support/
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                var response = await client.SendAsync(request);
                return await response.Content.ReadAsStringAsync();
            }

        }

        async static Task<string> GetAnswers()
        {
            var uri = host + service + method;
            Console.WriteLine("Calling " + uri + ".");
            string response = "NULL";
            try
            {
                response = await Post(uri, question);
                Console.WriteLine(response);
                Console.WriteLine("Press any enter to continue.");
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine(e.InnerException.Message);
            }
            return response;

        }
        [WebMethod]
        public string QnAMakerQuestionsAnswers()
        {
            return GetAnswers().Result;
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...