web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Unanswered

C# wrapper Async method not executing AX 2012 method but sync is working

(0) ShareShare
ReportReport
Posted on by

I am trying to call my AX 2012 method async using C# wrapper but the AX 2012 method is not executing. The same data is correctly processed by sync call and is executing properly. Below is the code for Controller in C#. Why is it not executing async method ?

I have tried to call it without await as well as await. I tried debugging but it wont probably work due to different thread.

The sync part is working properly but the MS Flow calling this API times out due to limitations in Flow.

Can you please tell me how to make the async work? Can I make it async in AX 2012 ?

//It dynamically sets the AIF Url and calls the method to consume AIF services
using SXA_FlowIntegrationWebAPI.Helpers;
using SXA_FlowIntegrationWebAPI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace SXA_FlowIntegrationWebAPI.Controllers
{
public class MainController : ApiController
{
    [ActionName("Method")]
    [HttpPost]
    [Authorize]
    public IHttpActionResult CallMethod(ClassMethodCallRequest request)
    {
        dynamic retValue;
        using (var client = new AX2012.SXAFlowIntegrationServiceClient())
        {
            try
            {
                var callContext = new AX2012.CallContext();
                callContext.Company = request.companyId;
                callContext.LogonAsUser = User.Identity.Name;

                client.InnerChannel.OperationTimeout = new TimeSpan(0, 10, 00);
                client.Endpoint.Address = new System.ServiceModel.EndpointAddress(request.aifURL);
                if (request.methodName == "async")
                {
                    retValue = "";
                    //client.callMethodAsync(callContext, request.className, request.methodName, request.dataJsonStr);
                    CallMethodAsyncCustom(client, callContext, request);
                }
                else
                {
                    retValue = client.callMethod(callContext, request.className, request.methodName, request.dataJsonStr);
                }
            }
            catch(Exception e)
            {
                return Json(new ClassMethodCallResponse
                {
                    status = "Error",
                    userName = User.Identity.Name,
                    className = request.className,
                    methodName = request.methodName,
                    aifURL = request.aifURL,
                    error = e
                });
            }
        }
        return Json(new ClassMethodCallResponse
        {
            status = "Success",
            userName = User.Identity.Name,
            className = request.className,
            methodName = request.methodName,
            aifURL = request.aifURL,
            data = retValue,
        });
    }

    public static async System.Threading.Tasks.Task CallMethodAsyncCustom(AX2012.SXAFlowIntegrationServiceClient client, AX2012.CallContext callContext, ClassMethodCallRequest request)
    {
         await  System.Threading.Tasks.Task.Run(() => client.callMethodAsync(callContext, request.className, request.methodName, request.dataJsonStr));
         return "Completed";
    }
}

I have the same question (0)
  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    One problem with your code is that you never look at what the method did. For instance, you don't whether it threw an exception. You should either await the method or look at the result of the task. Also, couldn't 'client' object get disposed before the async method executes?

    Visual Studio surely can debug multi-threaded applications.

    Note that your question how to use C#, not AX. Acording to yourself, AX doesn't get called by your code at all. Therefore you should rather ask in a C# forum.

  • Community Member Profile Picture
    on at

    I have created a new error log table for this in AX to check for errors since I am unaware of polling in C# and MS Flow Custom connector.

    Can I use async method call in AX 2012?

  • Community Member Profile Picture
    on at

    Also it would be helpful if you could explain the correct way to dispose and create the client. I am using async and sync both calls depending on a condition.

  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    I think the easiest way would be using 'await'. Then the system would wait for completion the operation before disposing the client.

  • Community Member Profile Picture
    on at

    Hi martin,

    I tried using 'await' in controller class too before calling my method "CallMethodAsyncCustom".

    For that, I had to change the method name from public IHttpActionResult CallMethod(ClassMethodCallRequest request) to public async Task<IHttpActionResult> CallMethod(ClassMethodCallRequest request).

    On running that, my MS flow throwed error of "Failed to logon to Microsoft Dynamics AX". It was caught in my catch block.

    Also, I want to use Async manner only for a specific method name or condition.

  • Martin Dráb Profile Picture
    237,880 Most Valuable Professional on at

    Good, it seems that your problem about how to call it asynchronously has been resolved. Now you'll have to look at the problem with logging in. Feel free to create a thread for that if needed. By the way, if you try to test everything at once, you won't know which part failed. Therefore you should test your method first and only then integrated with Flow or whatever you need.

    You need to decide at design time whether a method will be declared asynchronous or not. You can't do that at runtime. But you can call synchronous methods from asynchronous and asynchronous in synchronous way. But that a topic for a C# forum, not an AX forum.

  • Community Member Profile Picture
    on at

    Thanks Martin,

    AX 2012 code was not executing as the object was getting disposed.

    Instead of using 'using' keyword, I used client.Open and client.Close and my method was getting executed .

    Though it was not executing as required and was not exactly working in async manner, I have opened a new thread in C# forum for that.

    csharpforums.net/.../

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 584 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 499 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 254 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans