[DotNet] Call async method from C/AL synchronously

toLLtotoLLto Member Posts: 34
Is there any way to call some DotNet async method from C/AL synchronously?

In C# it can be done like this:

Method definition:
public async Task<string> GetDataAsync(string parameter){}
Synchronous call:
string result = somObject.GetDataAsync(parameter).Result;

Unfortunately I can't translate this to C/AL. Property "Result" is not available, and without this part method is called asynchronously. Somehow I need to call this method synchronously, wait until it finishes and grab the result.

Answers

  • nhsejthnhsejth Member, Microsoft Employee Posts: 34
    Hi,

    The AL language does not support async .Net operations. If you must use an async function, you have to encapsulate it in a wrapper that hides the async behavior.
    _________________
    Niels-Henrik Sejthen
    Senior Software Developer
    Microsoft Dynamics NAV

    The information in this post is provided "AS IS" with no warranties, and confers no rights. This post does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion.
  • EvREvR Member Posts: 178
    edited 2017-04-13
    Unfortunately that's not possible. Not because async is not supported, but because those extension methods are not available. I would just write a small .net assembly that takes care of it and use it in NAV.
    Or if you really want to, check if you can access it using reflection? But I'm not sure that would work.
  • vytjakvytjak Member Posts: 36
    Could you give a few more details, i.e. NAV version, and maybe a snippet of code of what you are trying to do?

    Checking one of our projects, I can see we have used the Result property on asyncrhonous method calls successfully, like this:
    HttpResponse := HttpClient.PostAsync(ServiceURL, StringContent).Result;
    IF (HttpResponse.IsSuccessStatusCode) THEN BEGIN
      ResponseData := HttpResponse.Content.ReadAsStringAsync.Result;
    [...]
    
    Vytenis Jakas
    B3 Technologies - Making Technology Serve the People
Sign In or Register to comment.