我有一个方法在如下循环中被异步调用
I have a method that is called asynchonously as part of a loop as below
List<Task> tasks = new List<Task>(); foreach (var cust in e.customers) { tasks.Add(Task.Run(() => exMan.perfomAction(cust, userId))); } await Task.WhenAll(tasks);此方法调用服务器其他方法.这些方法之一进行服务调用.
This method calls serveral other methods. One of these methods makes a service call.
我得到了错误.
线程被中止.
应用程序并非总是在同一行代码上出错.可能是在初始化服务引用,设置其属性之一或在Im进行调用时.堆栈跟踪因代码行引发异常而有所不同
The application doesn't always error on the same line of code. It might be when the service reference is being initialized, when one of it's properties is being set, or when Im making the call. The stack trace is different dependent on what line of code throws the exception
当我同步调用此方法时,它可以正常工作,调用服务并获得响应.当我异步调用服务时,出现错误
When I call this method synchonously it works fine, calls the service and gets a response. When I call the service Asynchonously I get the error
到目前为止,Google尚无法为我提供任何帮助
Google has not been able to help me so far, any ideas
根据要求,这是应用程序调用服务时发生错误时的堆栈跟踪(此变化取决于代码行引发异常)
As requested here's the stacktrace when the app errors from calling the service (this changes dependent on what line of code throws the exception)
代码行错误:
var axPriceList = client.findEx(callContext, queryCriteria, documentContext);Stacktrace:
Stacktrace:
at System.ServiceModel.Channels.Binding.EnsureInvariants(String contractName) at System.ServiceModel.Description.ServiceEndpoint.EnsureInvariants() at System.ServiceModel.Channels.ServiceChannelFactory.BuildChannelFactory(ServiceEndpoint serviceEndpoint, Boolean useActiveAutoClose) at System.ServiceModel.ChannelFactory.CreateFactory() at System.ServiceModel.ChannelFactory.OnOpening() at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) at System.ServiceModel.ChannelFactory.EnsureOpened() at System.ServiceModel.ChannelFactory`1.CreateChannel(EndpointAddress address, Uri via) at System.ServiceModel.ChannelFactory`1.CreateChannel() at System.ServiceModel.ClientBase`1.CreateChannel() at System.ServiceModel.ClientBase`1.CreateChannelInternal() at System.ServiceModel.ClientBase`1.get_Channel() at PriceListManagement.Domain.PricePriceListService.PriceListServiceClient.PriceListManagement.Domain.PricePriceListService.PriceListService.findEx(PriceListServiceFindExRequest request) in C:\Users\Richard\Documents\tfs\PriceListManagement\PriceListManagementAx2012\PriceListManagement.Domain\Service References\PricePriceListService\Reference.cs:line 1964 推荐答案因此,我进行了两项更改,这些更改似乎有所不同,并且按预期运行.服务调用已异步进行,正在等待.
So I've made two changes that seems to of made a difference and is working as expected. The service call has been made async and is awaited.
var axPriceList = await client.findExAsync(callContext, queryCriteria, documentContext);现在等待调用包含以下代码的方法的根调用
The root call which calls the method containing the code below is now awaited
private async Task CallingMethod(string cust, string userId) { await MehodA(string cust, string userId); } private async Task MehodA(string cust, string userId) { List<Task> tasks = new List<Task>(); using (var throttler = new SemaphoreSlim(10)) { foreach (var cust in e.customers) { tasks.Add(tasks.Add(Task.Run(async () => { try { await exMan.perfomAction(cust, userId); } finally { throttler.Release(); } })); } } await Task.WhenAll(tasks); }更多推荐
从异步方法进行服务调用时线程被中止错误
发布评论