Search code examples
c#apinlpreturn

Google NLP Api C# an System.AggregateException occured


I try to filter a line of text by using the NLP Api from Google.

But once I try to return a value an error message occured.

I already tried to debug the code but it will not go futher than the creation of the variable "google". After this the program returns the error message saying "An System.AggregateException occured in MESSAGE_CREATED." What basically means that multiple error's occured.

Does someone onderstand why this is happening, or how I can fix this?

    public dynamic DataFilter(string data)
    {
        var client = LanguageServiceClient.Create();

        var response = client.AnalyzeSentiment(new Document()
        {
            Content = data,
            Type = Document.Types.Type.PlainText
        });

        var sentiment = response.DocumentSentiment;
        return sentiment;
    }

    private async Task AnalyzeMessage(MessageCreateEventArgs e)
    {
        string retrievedData = e.Message.Content;
        string userMessage = retrievedData.ToLower();

        if(!e.Author.IsBot)
        {
            if(userMessage.Contains("nlp"))
            {
                string line = "What is the capital of the Netherlands";

                var google = DataFilter(line);

                await e.Channel.SendMessageAsync($"The value given back is {google.Magnitude}");
            }
        }
    }

Solution

  • I found the problem!

    The problem was not in the code but in the google api. The api returns an "System.AggregateException" because the environment variable was not added into the application (I added it into my computer but this was not enough).

    Go to "Solution Explorer" > "Right click on your project" > "Properties" > "Debug" and add under name "GOOGLE_APPLICATION_CREDENTIALS" and as value the location of to your authentication JSON file (you can download this on https://cloud.google.com/natural-language/docs/quickstart-client-libraries)

    I hope this helps for someone else!