Microsoft Translation API for Winform

Inventerio Soft 1 Reputation point
2021-02-27T10:37:06.897+00:00

I'm trying to use the Microsoft Translation API in VB.Net, I'm using the Microsoft sample codes (I converted the code from C# to VB.Net), it was working fine until a month ago, but now, it's giving an error.
Does anyone know the solution for this?
Thanks in advance

The code I used:

Private Async Sub ArabicTranslation()  
    Dim route As String = "/translate?api-version=3.0&to=ar"  
    Dim textToTranslate As String = txtProductName.Text  
    Dim body As Object() = New Object() {New With {Key .Text = textToTranslate}}  
    Dim requestBody = JsonConvert.SerializeObject(body)  
    Using client = New HttpClient()  
        Using request = New HttpRequestMessage()  
            request.Method = HttpMethod.Post  
            request.RequestUri = New Uri(endpoint_var & route)  
            request.Content = New StringContent(requestBody, Encoding.UTF8, "application/json")  
            request.Headers.Add("Ocp-Apim-Subscription-Key", key_var)  
            request.Headers.Add("Ocp-Apim-Subscription-Region", region_var)  
            Dim response As HttpResponseMessage = Await client.SendAsync(request).ConfigureAwait(False)  
            Dim result As String = Await response.Content.ReadAsStringAsync()  
            Dim deserializedOutput As TranslationResult() = JsonConvert.DeserializeObject(Of TranslationResult())(result)  
            For Each o As TranslationResult In deserializedOutput  
                For Each t As Translation In o.Translations  
                    txtArabicName.Text = t.Text  
                Next  
            Next  
        End Using  
    End Using  
End Sub  

72597-2.png

Azure Translator
Azure Translator
An Azure service to easily conduct machine translation with a simple REST API call.
393 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,915 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,724 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ken Tucker 5,851 Reputation points
    2021-02-27T11:15:26.517+00:00

    I would look at the Json you are receiving from the Translation Service. The error sounds like you are only getting one TranslationResult instead of an array of TranslationResult. Put a breakpoint on line 1159 and look at what result is.
    You might have to copy result into the clipboard and use paste special --> paste json as classes to update the class you are deserializing into

    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.