客户端异步调用wcf服务。关闭连接导致同步行为

时间:2016-11-04 10:33:19

标签: c# wcf asynchronous

我正在从客户端异步调用WCF服务。

int i = 0;
Service1Client client = (Service1Client)_container.Resolve<IService1>(new ParameterOverride("remoteAddress", url),
                new ParameterOverride("endpointConfigurationName", "basicEndpoint"));

        try
        {
            client.Open();
            foreach (var fileInstance in list)
            {
                i++;
                _logger.Debug(() => string.Format("Call {0} for url {1} for fileId: {2}", i, url, fileInstance.FileId));

                var request = new ContextRequest
                {
                    FileDetails = new FileDetails
                    {
                        FileName = fileInstance.Filename,
                        FileId = fileInstance.FileId
                    }
                };
                var contextRequest = new Context { Request = request};

                client.ValidateAsync(contextRequest).ContinueWith(AsyncMethodException, contextRequest, TaskContinuationOptions.OnlyOnFaulted);
            }
        }
        finally
        {
            if (client.State != System.ServiceModel.CommunicationState.Faulted)
            {
                _logger.Debug("Closing client");
                //client.ChannelFactory.Close();
                client.Close();
            }
            else
            {
                _logger.Debug("Aborting client");
                client.Abort();
            }
        }

        _logger.Debug("All file instances completed: {0}", list.Count);
    }

从日志中我可以看到客户端是异步调用的..但是它似乎要等到客户端关闭(wcf服务完成处理10

2016-11-03 16:43:39.4641 DEBUG Call 1 for url http://x/y.svc for fileId: 24
2016-11-03 16:43:39.4641 DEBUG Call 3 for url http://x/y.svc  for fileId: 26
2016-11-03 16:43:39.4641 DEBUG Call 2 for url http://x/y.svc  for fileId: 25
2016-11-03 16:43:39.4761 DEBUG Call 4 for url http://x/y.svc  for fileId: 27
2016-11-03 16:43:39.4761 DEBUG Call 5 for url http://x/y.svc  for fileId: 28
2016-11-03 16:43:39.4761 DEBUG Call 6 for url http://x/y.svc  for fileId: 29
2016-11-03 16:43:39.4761 DEBUG Call 7 for url http://x/y.svc  for fileId: 30
2016-11-03 16:43:39.4761 DEBUG Call 8 for url http://x/y.svc  for fileId: 31
2016-11-03 16:43:39.4961 DEBUG Call 9 for url http://x/y.svc  for fileId: 32
2016-11-03 16:43:39.4961 DEBUG Call 10 for url http://x/y.svc  for fileId: 33
2016-11-03 16:43:39.4961 DEBUG Closing client
2016-11-03 16:44:26.6591 DEBUG All file instances completed: 10

有关如何确保我可以在完成服务时关闭连接但没有等待的任何想法?我可以收听回电吗?

0 个答案:

没有答案