C# 包装器异步方法未执行 AX 2012 方法但同步正在运行

问题描述

我正在尝试使用 C# 包装器异步调用我的 AX 2012 方法,但 AX 2012 方法未执行。同步调用正确处理了相同的数据并正确执行。下面是 C# 中控制器的代码。为什么不执行异步方法

我尝试在不等待和等待的情况下调用它。我尝试过调试,但由于线程不同,它可能无法正常工作。

同步部分工作正常,但由于 Flow 的限制,调用此 API 的 MS Flow 超时。

你能告诉我如何使异步工作吗?我可以在 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.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",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.dataJsonStr));
         return "Completed";
    }
}

}

解决方法

Controller 方法需要是 async Task<IHttpActionResult> 返回类型,那么你也需要等待控制器方法中的调用。

,

我认为通过使用 'using' 关键字,对象在我的异步函数执行完成之前就被释放了。我在异步函数中使用了 client.Open() 和 client.Close(),它进入了 AX 2012 方法。

虽然,我没有解决我的问题,但至少我能够访问该方法。 我有一个问题,即 async 没有以 asymc 方式运行,为此我在 C# 论坛中打开了我的帖子。

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...