Dhruvil
Member
- Joined
- Dec 19, 2020
- Messages
- 12
- Programming Experience
- 3-5
I am getting this error when I run my C# wrapper from Power Automate. But this error is returned only sometimes. On resubmitting my power automate flow, the code works as required.
This temporary error is disrupting long processes sometimes and has to be resubmitted.
My code for C# controller is :
My guess is this error has started popping after I added this line : client.InnerChannel.OperationTimeout = new TimeSpan(0, 10, 00);
Since, the request was getting expired in 1 min, adding this line was necessary.
Can you please tell why am I getting that communication error sometimes ?
C#:
{
"Message": "An error has occurred.\r\nclientRequestId: 66b3b4af-edc0-4bbb-b6e2-11e217c00480",
"ExceptionMessage": "The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.",
"ExceptionType": "System.ServiceModel.CommunicationObjectFaultedException",
"StackTrace": "\r\nServer stack trace: \r\n at System.ServiceModel.Channels.CommunicationObject.Close(TimeSpan timeout)\r\n\r\nException rethrown at [0]: \r\n at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)\r\n at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)\r\n at System.ServiceModel.ICommunicationObject.Close(TimeSpan timeout)\r\n at System.ServiceModel.ClientBase`1.System.ServiceModel.ICommunicationObject.Close(TimeSpan timeout)\r\n at SXA_FlowIntegrationWebAPI.Controllers.MainController.CallMethod(ClassMethodCallRequest request)\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"
}
This temporary error is disrupting long processes sometimes and has to be resubmitted.
My code for C# controller is :
//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 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"; } }:
//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";
}
}
My guess is this error has started popping after I added this line : client.InnerChannel.OperationTimeout = new TimeSpan(0, 10, 00);
Since, the request was getting expired in 1 min, adding this line was necessary.
Can you please tell why am I getting that communication error sometimes ?