繁体   English   中英

C# 包装异步方法不执行 AX 2012 方法但同步工作

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

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

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

同步部分工作正常,但由于 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.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";
    }
}

}

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

我认为通过使用“使用”关键字,object 在我的异步 function 执行完成之前就被处理掉了。 我在我的异步 function 中使用了 client.Open() 和 client.Close() 并进入了 AX 2012 方法。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM