簡體   English   中英

Google Dialogflow V2 webhook 不起作用 - InvalidType 和 Null 異常

[英]Google Dialogflow V2 webhook not working - InvalidType and Null Exception

我正在按照谷歌示例使用 Google Dailog Flow cersion 2 api 為對話流創建 .net 核心 3.0 webhook。 這是代碼

public class DialogflowController : ControllerBase
{
   private static readonly JsonParser jsonParser =
        new JsonParser(JsonParser.Settings.Default.WithIgnoreUnknownFields(true));

    public ContentResult DialogAction()
    {
        WebhookRequest request;
        using (var reader = new StreamReader(Request.Body))
        {
            request = jsonParser.Parse<WebhookRequest>(reader);
        }

        // Note: you should authenticate the request here.

        // Populate the response
        WebhookResponse response = new WebhookResponse
        {
            // ...
        };

        string responseJson = response.ToString();
        return Content(responseJson, "application/json");
    }
}

我正在使用 SoapUI 創建 rest 服務,並使用從 dialogflow 本身復制的發布請求測試代碼。 運行時我收到以下錯誤:

---> System.TypeInitializationException: The type initializer for 'Google.Cloud.Dialogflow.V2.ContextReflection' threw an exception.
 ---> System.ArgumentNullException: Value cannot be null. (Parameter 'extension')

任何想法為什么或我在這里缺少什么?

謝謝

我的項目目前在 .NET Core 3.1 上,我必須更改代碼以從 StreamReader 異步讀取。 我不得不改變這個:

using (var reader = new StreamReader(Request.Body))
{
     request = jsonParser.Parse<WebhookRequest>(reader);
}

有了這個:

using (var reader = new StreamReader(Request.Body))
{
     request = jsonParser.Parse<WebhookRequest>(await reader.ReadToEndAsync());
}

我還必須使用 async 關鍵字更新方法簽名並將 ContentResult 放入任務中。

公共異步任務 DialogAction()

希望這有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM