使用WebMethod C#调用QnAMaker时无限加载

时间:2019-06-10 07:00:25

标签: c# azure web-services qnamaker

当我尝试调用Web方法“ 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;
        }
    }
}

0 个答案:

没有答案