question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Dialogflow] WebhookRequest parse exception in .NET Core app

See original GitHub issue

Environment details

  • OS: Windows 10
  • .NET version: .NET Core 3.1
  • Package name and version:
    • Google.Cloud.Dialogflow.V2 1.2.0;
    • Google.Protobuf 3.11.4.

Steps to reproduce

  1. Run the sample from https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.Dialogflow.V2/index.html for .NET Core;
  2. When calling the following line in the sample:
request = jsonParser.Parse<WebhookRequest>(reader);

I get the following exception:

Synchronous operations are disallowed. Call ReadAsync or set AllowSynchronousIO to true instead.

To try to fix this issue I included the following lines in my ConfigureServices method in the Startup class:

services.Configure<KestrelServerOptions>(options =>
{
	options.AllowSynchronousIO = true;
});
																		
services.Configure<IISServerOptions>(options =>
{
	options.AllowSynchronousIO = true;
});

However after doing this I start getting this exception:

Unexpected end of document in state: StartOfDocument

   at Google.Protobuf.JsonTokenizer.JsonTextTokenizer.ValidateState(State validStates, String errorPrefix)
   at Google.Protobuf.JsonTokenizer.JsonTextTokenizer.NextImpl()
   at Google.Protobuf.JsonTokenizer.Next()
   at Google.Protobuf.JsonParser.Merge(IMessage message, JsonTokenizer tokenizer)
   at Google.Protobuf.JsonParser.Merge(IMessage message, TextReader jsonReader)
   at Google.Protobuf.JsonParser.Parse[T](TextReader jsonReader)

The workaround I found for this was replacing:

request = jsonParser.Parse<WebhookRequest>(reader);

To:

string requestAsString;
// (...)
requestAsString = await reader.ReadToEndAsync();
request = jsonParser.Parse<WebhookRequest>(requestAsString);

Here is the request I’m getting from DialogFlow:

{
  "responseId": "55727bcc-7d89-4fd5-8c1f-20f9e4d8d19c-7f245a81",
  "queryResult": {
    "queryText": "Meu nome é Fagner",
    "parameters": {
      "name": "Fagner"
    },
    "allRequiredParamsPresent": true,
    "fulfillmentMessages": [{
      "text": {
        "text": [""]
      }
    }],
    "outputContexts": [{
      "name": "projects/<project id>/agent/sessions/<session id>/contexts/actions_capability_account_linking",
      "parameters": {
        "name": "Fagner",
        "name.original": "Fagner"
      }
    }, {
      "name": "projects/<project id>/agent/sessions/<session id>/contexts/actions_capability_audio_output",
      "parameters": {
        "name": "Fagner",
        "name.original": "Fagner"
      }
    }, {
      "name": "projects/<project id>/agent/sessions/<session id>/contexts/actions_capability_media_response_audio",
      "parameters": {
        "name": "Fagner",
        "name.original": "Fagner"
      }
    }, {
      "name": "projects/<project id>/agent/sessions/<session id>/contexts/actions_capability_screen_output",
      "parameters": {
        "name": "Fagner",
        "name.original": "Fagner"
      }
    }, {
      "name": "projects/<project id>/agent/sessions/<session id>/contexts/google_assistant_input_type_voice",
      "parameters": {
        "name": "Fagner",
        "name.original": "Fagner"
      }
    }, {
      "name": "projects/<project id>/agent/sessions/<session id>/contexts/__system_counters__",
      "lifespanCount": 1,
      "parameters": {
        "no-input": 0.0,
        "no-match": 0.0,
        "name": "Fagner",
        "name.original": "Fagner"
      }
    }],
    "intent": {
      "name": "projects/<project id>/agent/intents/<session id>",
      "displayName": "greetings-intent"
    },
    "intentDetectionConfidence": 1.0,
    "languageCode": "en"
  },
  "originalDetectIntentRequest": {
    "source": "google",
    "version": "2",
    "payload": {
      "user": {
        "locale": "en-US",
        "lastSeen": "2020-02-22T23:44:35Z",
        "userVerificationStatus": "VERIFIED"
      },
      "conversation": {
        "conversationId": "<conversation id>",
        "type": "ACTIVE",
        "conversationToken": "[\"__system_counters__\"]"
      },
      "inputs": [{
        "intent": "actions.intent.TEXT",
        "rawInputs": [{
          "inputType": "VOICE",
          "query": "Meu nome é Fagner"
        }],
        "arguments": [{
          "name": "text",
          "rawText": "Meu nome é Fagner",
          "textValue": "Meu nome é Fagner"
        }]
      }],
      "surface": {
        "capabilities": [{
          "name": "actions.capability.ACCOUNT_LINKING"
        }, {
          "name": "actions.capability.AUDIO_OUTPUT"
        }, {
          "name": "actions.capability.MEDIA_RESPONSE_AUDIO"
        }, {
          "name": "actions.capability.SCREEN_OUTPUT"
        }]
      },
      "isInSandbox": true,
      "availableSurfaces": [{
        "capabilities": [{
          "name": "actions.capability.WEB_BROWSER"
        }, {
          "name": "actions.capability.AUDIO_OUTPUT"
        }, {
          "name": "actions.capability.SCREEN_OUTPUT"
        }]
      }],
      "requestType": "SIMULATOR"
    }
  },
  "session": "projects/<project id>/agent/sessions/<session id>"
}

Can we update the docs with my fix?

Also, I will report this issue in the protobuf repo to keep you in the loop for when this is fixed.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
jskeetcommented, Feb 24, 2020

Righto - I’ve got a PR out to update the docs to show the ReadToEndAsync version, so let’s just not worry about the other exception 😃

1reaction
jskeetcommented, Feb 24, 2020

(I don’t understand why allowing synchronous IO doesn’t just make it work, but it’s simpler to document the workaround.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Google Dialogflow V2 webhook not working
My project is on .NET Core 3.1 at the moment, and I had to make a change to the code to asynchronously read...
Read more >
.NET client library | Google Cloud
Web hook template code for ASP.NET Core (synchronous parsing) // the action robust against new fields being introduced by Dialogflow. // Parse ......
Read more >
Dialogflow fulfillment with C# and App Engine - Mete Atamel
Notice how the HTTP request body is parsed as a WebhookRequest. This is the request Dialogflow sends to our code. In response, we...
Read more >
[Solved]-Google Dialogflow V2 webhook not working
My project is on .NET Core 3.1 at the moment, and I had to make a change to the code to asynchronously read...
Read more >
Create a C# .net core webhook for a Dialogflow chatbot
I'm using VS 2019 and will be adding an C# Asp.net Core Web Application ... from the Dialogflow api, parse that request and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found