[DotNet] Call async method from C/AL synchronously
toLLto
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:
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.
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.
0
Answers
-
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.0 -
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.0 -
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 People1
Categories
- All Categories
- 75 General
- 75 Announcements
- 66.7K Microsoft Dynamics NAV
- 18.8K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 610 NAV Courses, Exams & Certification
- 1.9K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 251 Dynamics CRM
- 103 Dynamics GP
- 6 Dynamics SL
- 1.5K Other
- 991 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 28 Design Patterns (General & Best Practices)
- Architectural Patterns
- 9 Design Patterns
- 4 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1K General Chat
- 1.6K Website
- 77 Testing
- 1.2K Download section
- 23 How Tos section
- 249 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions

