【问题标题】:C# wrapper Async method not executing AX 2012 method but sync is workingC# 包装器异步方法不执行 AX 2012 方法但同步工作
【发布时间】:2020-12-21 23:15:56
【问题描述】:

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

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

同步部分工作正常,但调用此 API 的 MS Flow 由于 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.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";
    }
}

}

【问题讨论】:

  • 只是为了排除显而易见的问题,您是在 AX 中进行增量还是填充 CIL 编译?
  • 我执行了增量

标签: c# web-services dynamics-ax-2012 power-automate


【解决方案1】:

控制器方法需要是async Task&lt;IHttpActionResult&gt;返回类型,那么你也需要在控制器方法中等待调用。

【讨论】:

  • 我尝试使用它,但我的 MS Flow 失败并出现错误“无法登录到 Microsoft Dynamics AX”。此外,我希望它对于特定条件是异步的。不是整个方法
  • 方法在编译时被标记为异步。如果你想要一个非异步方法,你必须创建另一个控制器方法。
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多