如何解决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/
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。