Warm tip: This article is reproduced from serverfault.com, please click

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

发布于 2020-12-18 17:56:59

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<string> 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";
    }
}

}

Questioner
Dhruvil Sheth
Viewed
0
Dhruvil Sheth 2020-12-21 23:15:56

I figured it that by using the 'using' keyword, the object was getting disposed before the completion of execution of my async function. I used client.Open() and client.Close() in my async function and it went into the AX 2012 method.

Though, I didn't solve my issue, atleast I was able to access the method. I have a issue that async is not running in asymc manner and I have opened my thread in C# forum for that.

https://csharpforums.net/threads/async-c-code-not-executing-in-async-manner.6281/