DeepLcom / deepl-dotnet

Official .NET library for the DeepL language translation API.
MIT License
177 stars 25 forks source link

Nothing is happening #15

Open grandeljay opened 2 years ago

grandeljay commented 2 years ago

I am new to asynchronous programming in .net so I'm hoping that I am making an obvious mistake here. Nothing is happening when I Await Translator

Dim AuthKey As String = "01234567-89ab-cdef-0123-456789abcdef:fx" ' Free API Key
Dim Translator As New Translator(AuthKey)
Dim Options As New TextTranslateOptions With {
    .Formality = Formality.More,
    .TagHandling = "html",
    .SentenceSplittingMode = SentenceSplittingMode.NoNewlines
}

Dim TranslatedText = Await Translator.TranslateTextAsync(
    TextToTranslate,
    FormMain.Settings.LanguageSource.DeepLLanguageCode,
    FormMain.Settings.LanguageTarget.DeepLLanguageCode,
    Options
)

EDIT: To clarify, I literally mean nothing, there is no error and the code also doesn't continue to execute. It just waits forever after that call.

Please advise. Thanks! - Jay

daniel-jones-deepl commented 2 years ago

Hi Jay, thanks for creating this issue.

It seems like your code is correct. Do you have this code placed within an Async function?

To rule out other problems, you could try another async function known to work, for example System.Io.File.WriteAllTextAsync (docs here).

If that works as expected, there could be a problem with your internet connection. This .NET library retries failed requests automatically, so to get a failure faster you could try lowering the number of retries as explained here.

grandeljay commented 2 years ago

Hey Daniel,

the file is created, I am just very confused about the debugger not continuing.

Here are some details about the code I am using:

Public Shared Function GetTranslationForPO(TextToTranslate As String, Optional Context As String = Nothing) As String
    Return GetTranslationForPOAsync(TextToTranslate, Context).Result
End Function

Private Shared Async Function GetTranslationForPOAsync(TextToTranslate As String, Context As String) As Task(Of String)
    ' [truncated version]

    Dim AuthKey As String = "01234567-89ab-cdef-0123-456789abcdef:fx" ' Free API Key
    Dim Translator As New Translator(AuthKey)
    Dim Options As New TextTranslateOptions With {
        .Formality = Formality.More,
        .TagHandling = "html",
        .SentenceSplittingMode = SentenceSplittingMode.NoNewlines
    }

    ' Debugging seems to stop after this step
    Await IO.File.WriteAllTextAsync("right-here.txt", "This file is created successfully.")    

    ' I am expecting this to be the next step
    Dim TranslatedText = Await Translator.TranslateTextAsync(
        TextToTranslate,
        FormMain.Settings.LanguageSource.DeepLLanguageCode,
        FormMain.Settings.LanguageTarget.DeepLLanguageCode,
        Options
    )

    ' I never get here while debugging
    Return TextToTranslate
End Function

Would love some more insight! Thanks.

daniel-jones-deepl commented 2 years ago

Okay, then if you get the same debugging problem with a system function, it might be something unrelated to the DeepL library.

I am not very familiar with Visual Basic code, but it seems like in this StackOverflow thread someone has a similar problem. The answer from Stephen Cleary suggests your problem could be that your program is exiting before the async functions complete? Although you did say the program waits, rather than that it stops. Calling .Result is also equivalent to a .Wait() call, so I am not sure.

Another idea: do you step through the code line-by-line while debugging, or do you set breakpoints? Breakpoints may work better, because some debuggers lose the execution line with async code.

grandeljay commented 2 years ago

do you step through the code line-by-line while debugging, or do you set breakpoints?

I've tried both with the same result. I am familiar with async programming in general (i. e. JavaScript) but in vb.net it's just confusing for me, I really don't understand this behaviour, it doesn't make any sense to me. 😞

The answer from Stephen Cleary suggests your problem could be that your program is exiting before the async functions complete?

My program is not exiting, it's still running and seems to just sit still and Await for eternity.

I've spent so much time the last days trying to find something online but no luck yet 😖

daniel-jones-deepl commented 2 years ago

Hi @grandeljay, I looked into this but I cannot reproduce what you saw, using JetBrains Rider. In debug mode I am also able to step through this code without issue. Here is the code I used (in a file Program.vb):

Imports System
Imports DeepL

Module Program
    Sub Main(args As String())
        Console.WriteLine(GetTranslationForPO("Hello"))
        MainAsync(args).Wait()
    End Sub

    Private Async Function MainAsync(args As String()) As Task
        Dim result = Await GetTranslationForPOAsync("Hello")
        Console.WriteLine(result)
    End Function

    Public Function GetTranslationForPO(TextToTranslate As String) As String
        Return GetTranslationForPOAsync(TextToTranslate).Result
    End Function

    Private Async Function GetTranslationForPOAsync(TextToTranslate As String) As Task(Of String)
        Dim AuthKey As String = "01234567-89ab-cdef-0123-456789abcdef:fx" ' Free API Key
        Dim Translator As New Translator(AuthKey)

        Dim Options As New TextTranslateOptions With {
                .Formality = Formality.More,
                .TagHandling = "html",
                .SentenceSplittingMode = SentenceSplittingMode.NoNewlines
                }

        Dim TranslatedText = Await Translator.TranslateTextAsync(TextToTranslate, "en", "de", Options)
        Return TranslatedText.Text
    End Function
End Module

The expected output is "Hallo" twice (Hello in German). If you do not get that output with the code above, could you give more details about your OS, IDE, and platform?

AdrianEfford commented 2 years ago

Hi together (@daniel-jones-deepl , @grandeljay) I'm having the same issue in Visual Studio 2022 (C#)... After trying to figure out what the issue exactly is, I got following exception: image

The Assembly 'Polly, Version=7.0.0.0' was not found Any ideas on what might solve this problem? Thank you in advance

grandeljay commented 2 years ago

@daniel-jones-deepl, thank you for all your efforts so far!

I looked into this but I cannot reproduce what you saw, using JetBrains Rider. In debug mode I am also able to step through this code without issue. Here is the code I used (in a file Program.vb):

It's the same behaviour for me, nothing is happening. Something seems to be wrong with my IDE or I am totally not understanding how to use it.

If you do not get that output with the code above, could you give more details about your OS, IDE, and platform?

OS: Windows 10 IDE: Microsoft Visual Studio Community 2022 (64-bit) Platform: VB.NET

daniel-jones-deepl commented 2 years ago

Hi @grandeljay, I just tested the Program.vb code above in Microsoft Visual Studio Community 2022 (64-bit) Version 17.2.4, on Windows 10, and unfortunately I get the expected output, so I can't reproduce your issue. Debugging also works as expected. I realized that my "platform" question was a bit vague, "Framework" is probably the right term. The Framework I tested with is .NET 6.0. I'm not sure if there is much else I can help with, there seems to be a problem with your system.

daniel-jones-deepl commented 2 years ago

Hi @AdrianEfford, Polly version 7.0.0.0 is a dependency of this DeepL library; normally it should be installed automatically when you install DeepL.NET. You could try installing Polly manually.

grandeljay commented 2 years ago

Thanks for all the suggestions!

I have tried resetting & reinstalling my IDE (Visual Studio Community 2022) and installing Polly but unfortunately nothing works. In the meantime I will be using the unofficial DeepL library, as this works without any problems for me. It also seems to use async functions but they are executing fine for me. I am really starting to think something is wrong with this library rather than my machine.

I am sorry I could not give you enough information to reproduce my issue, please let me know if I can ever try/test something for you, as I would much prefer to use the official DeepL library.

gszdev commented 2 years ago

Hi @grandeljay, I think you are faced with a deadlock and not an issue of deepl-dotnet library. I don't know if your are running a GUI based app. But if you using WinForms, WPF or ASP.NET MVC, ... you have a UI-Thread and which is internally working with a SynchronizationContext. Here are some links with infos to solve your issue:

C# Deadlocks in Depth – Part 2 ConfigureAwait FAQ

Maybe you can started with the following sample replacing the [Do] part:

Private Sub OnButtonClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
    Dim x As Integer
    Task.Run(Function()
                 x = [Do]().Result
             End Function).Wait()
End Sub

P.S: Here's a link to a C# => VB.NET converter Telerik Code Converter

dmki commented 11 months ago

Same thing in C# VS 2022. Unofficial lib works fine. Ugly hack would be to do something like this: TextResult[] result = null; Task.Run(() => { result = translator.TranslateTextAsync(texts, sourceLang, targetLang, translateOptions).Result; }).Wait();

(where translator is a configured instance of Translator.