异步C#代码未以异步方式执行

已加入
2020年12月19日
留言内容
12
编程经验
3-5
我为Ax 2012 AIF服务创建了一个C#包装器,该包装器由电源自动触发。电源自动化存在一个限制,网关会在110秒内超时。因此,我的要求是以异步方式调用C#包装器并立即获取响应,而不是等待任何响应(因为,我不知道在此使用轮询),并且通过在AX 2012端创建条目来处理任何错误(如果发生在AX 2012中)日志表。

主要问题在于,即使在使用了异步之后,流程仍在等待异步函数的响应。不仅如此,它还并行执行了两次我的AX代码。当我的流程超时时,第一个执行将停止,第二个执行即使在执行之后也将继续。我已经在表中插入了行,并且由于并行执行,即使已经对其进行了更新,它们也被创建了2次。

这是我的C#代码:

C#:
//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 == "insertUpdatePurchOrderAsync")
                        {
                            retValue = "";
                            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,
                             aif URL = request.aifURL,
                            error = e
                        });
                    }
                }

            return Json(new ClassMethodCallResponse
            {
                status = "Success",
                userName = User.Identity.Name,
                className = request.className,
                methodName = request.methodName,
                 aif URL = request.aifURL,
                data = retValue,
            });
        }

        public static System.Threading.Tasks.Task<SXA_FlowIntegrationWebAPI.AX2012.SXAFlowIntegrationServiceCallMethodResponse> callMethodAsyncNew(AX2012.SXAFlowIntegrationServiceClient client,SXA_FlowIntegrationWebAPI.AX2012.CallContext CallContext, string _className, string _methodName, string _data)
        {
            SXA_FlowIntegrationWebAPI.AX2012.SXAFlowIntegrationServiceCallMethodRequest inValue = new SXA_FlowIntegrationWebAPI.AX2012.SXAFlowIntegrationServiceCallMethodRequest();
            inValue.CallContext = CallContext;
            inValue._className = _className;
            inValue._methodName = _methodName;
            inValue._data = _data;
            return ((SXA_FlowIntegrationWebAPI.AX2012.SXAFlowIntegrationService)(client)).callMethodAsync(inValue);
        }

        public static async void CallMethodAsyncCustom(AX2012.SXAFlowIntegrationServiceClient client2, AX2012.CallContext callContext, ClassMethodCallRequest request)
        {
            var client = new AX2012.SXAFlowIntegrationServiceClient();
            try
            {
                client.Open();
                client.InnerChannel.OperationTimeout = new TimeSpan(0, 10, 00);
                client.Endpoint.Address = new System.ServiceModel.EndpointAddress(request.aifURL);
                client.Endpoint.Binding.SendTimeout = new TimeSpan(0, 10, 00);
                client.Endpoint.Binding.ReceiveTimeout = new TimeSpan(0, 20, 00);
                等待System.Threading.Tasks.Task.Run(()=>callMethodAsyncNew(client,callContext,request.className,request.methodName,request.dataJsonStr));
                //Console.WriteLine(x.response);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            finally
            {
                client.Close();
            }

        }
    }

}


我想要一种在完成异步方法之前返回JSON的方法。另外,应避免执行两次并行执行,并且只应执行一次直到结束。
 
Last edited:
已加入
2020年12月19日
留言内容
12
编程经验
3-5
所以你的意思不是 等待System.Threading.Tasks.Task.Run(()=>callMethodAsyncNew(client,callContext,request.className,request.methodName,request.dataJsonStr));
我应该用 System.Threading.Tasks.Task.Run(()=>callMethodAsyncNew(client,callContext,request.className,request.methodName,request.dataJsonStr)); ?
 

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,441
位置
弗吉尼亚州切萨皮克
编程经验
10+
这几乎将您带回了另一圈。  :)

You'll need to do an await within the lambda, but not await the Task created by Task.Run().
 
最佳 底部