microsoft / Cognitive-LinguisticAnalysis-Windows

Windows SDK for the Microsoft Linguistic Analysis API, part of Cognitive Services
https://www.microsoft.com/cognitive-services/en-us/linguistic-analysis-api
Other
30 stars 23 forks source link

Request just hangs #4

Open jconaty opened 7 years ago

jconaty commented 7 years ago

Request to get analyzer list just hangs. Specifically > return Client.ListAnalyzersAsync().Result This ultimately calls an http client request.

My code is directly from the sample app. Here is the default url in the LinquisticClient library private const string DefaultServiceHost = "https://api.projectoxford.ai/linguistics/v1.0";

Here is the class that I created

` public static class LinguisticAnalyzer { private static readonly LinguisticsClient Client = new LinguisticsClient("Removed_id");

    public static string Parse(string line)
    {
        // List analyzers
        Analyzer[] supportedAnalyzers = null;
        try
        {
            supportedAnalyzers = ListAnalyzers();
            var analyzersAsJson = JsonConvert.SerializeObject(supportedAnalyzers, Formatting.Indented, jsonSerializerSettings);
            Console.WriteLine("Supported analyzers: " + analyzersAsJson);
        }
        catch (Exception e)
        {
            Console.Error.WriteLine("Failed to list supported analyzers: " + e.ToString());
            Environment.Exit(1);
        }

        // Analyze text with all available analyzers
        var analyzeTextRequest = new AnalyzeTextRequest()
        {
            Language = "en",
            AnalyzerIds = supportedAnalyzers.Select(analyzer => analyzer.Id).ToArray(),
            Text = line //"Welcome to Microsoft Linguistic Analysis!"
        };

        try
        {
            var analyzeTextResults = AnalyzeText(analyzeTextRequest);
            var resultsAsJson = JsonConvert.SerializeObject(analyzeTextResults, Formatting.Indented, jsonSerializerSettings);
            Console.WriteLine("Analyze text results: " + resultsAsJson);

            return resultsAsJson;
        }
        catch (Exception e)
        {
            Console.Error.WriteLine("Failed to list supported analyzers: " + e.ToString());
            Environment.Exit(1);
        }

        return "";
    }

    /// <summary>
    /// Default jsonserializer settings
    /// </summary>
    private static JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()
    {
        DateFormatHandling = DateFormatHandling.IsoDateFormat,
        NullValueHandling = NullValueHandling.Ignore,
        ContractResolver = new CamelCasePropertyNamesContractResolver()
    };

    /// <summary>
    /// List analyzers synchronously.
    /// </summary>
    /// <returns>An array of supported analyzers.</returns>
    private static Analyzer[] ListAnalyzers()
    {
        try
        {
            return Client.ListAnalyzersAsync().Result;
        }
        catch (Exception exception)
        {
            throw new Exception("Failed to gather list of analyzers", exception as ClientException);
        }
    }

    /// <summary>
    /// Analyze text synchronously.
    /// </summary>
    /// <param name="request">Analyze text request.</param>
    /// <returns>An array of analyze text result.</returns>
    private static AnalyzeTextResult[] AnalyzeText(AnalyzeTextRequest request)
    {
        try
        {
            return Client.AnalyzeTextAsync(request).Result;
        }
        catch (Exception exception)
        {
            throw new Exception("Failed to analyze text", exception as ClientException);
        }
    }

}`
cthrash commented 7 years ago

Cannot repro. I know there were some issues with the servers earlier. It is possible this just works now?

jconaty commented 7 years ago

Hi Chris, Wish it was the case. No such luck. Are you using my class?

cthrash commented 7 years ago

Yes. The only change I made was to replace Removed_id with my own. I called LinguisticAnalyzer.Parse("This is a test"); from a console app.

jconaty commented 7 years ago

Hmm.. Im using the bot framework. I wonder if there is an issue with an async call or something.

cthrash commented 7 years ago

What happens if you change the async calls in the LinguisticsClient calls with (such as here) with .ConfigureAwait(false)?

We did something similar for a sister project (PR).

jconaty commented 7 years ago

The Magic happened! ConfigureAwait(false) is my new friend. Thank you sir!

miparnisari commented 6 years ago

I think this can be closed.