The socket connection was aborted - CommunicationException

后端 未结 7 688
我寻月下人不归
我寻月下人不归 2020-12-08 10:54

Originally:

  • I thought this was a circular reference problem........turns out it\'s not.
  • The problem arose from having not configured
相关标签:
7条回答
  • 2020-12-08 11:20

    This issue can also be due to not cleaning up the WCF client when your done using it. In our system, we use the disposable pattern along with wrapping all function calls into the system to allow for proper cleanup and logging. We use a version of the following class:

        public class WcfWrapper : IDisposable
        {
            private readonly OperationContextScope _operationContextScope;
            private readonly IClientChannel _clientChannel;
    
            public WcfWrapper(IClientChannel clientChannel)
            {
                _clientChannel = clientChannel;
                _operationContextScope = new OperationContextScope(_clientChannel);
            }
    
    
    
            public void Dispose()
            {
                _operationContextScope.Dispose();
            }
    
    
            public T Function<T>(Func<T> func)
            {
                try
                {
                    var result = func();
                    _clientChannel.Close();
                    return result;
                }
                catch (Exception ex)
                {
                    KTrace.Error(ex);
                    _clientChannel.Abort();
                    throw;
                }
    
            }
    
            public void Procedure(Action action)
            {
                try
                {
                    action();
                    _clientChannel.Close();
                }
                catch (Exception ex)
                {
                    KTrace.Error(ex);
                    _clientChannel.Abort();
                    throw;
                }
            }
        }
    
    }
    

    Every WCF call we make into our service is through a defined interface class like the following one:

        public sealed class WcfLoginManager : ILoginManager
        {
            private static LoginManagerClient GetWcfClient()
            {
                return 
                    new LoginManagerClient(
                        WcfBindingHelper.GetBinding(),
                        WcfBindingHelper.GetEndpointAddress(ServiceUrls.LoginManagerUri));
    
            }
    
            public LoginResponse Login(LoginRequest request)
            {
                using(var loginManagerClient = GetWcfClient())
                using (var slice = new WcfWrapper(loginManagerClient.InnerChannel))
                {
                    DSTicket ticket;
                    DSAccount account;
                    return slice.Function(() => new LoginResponse(loginManagerClient.Login(request.accountName, request.credentials, out ticket, out account), ticket, account));
                }
            }
        }
    

    Using this pattern, all WCF calls into the system are wrapped with either the Function or the Procedure method, allowing them to first ensure logging happens on all errors, and second to ensure the channel is closed when no errors occur but aborted if an exception happens. Finally, as it's in a using statement, the final dispose of the channel is called. In this way, errors that occur due to channels not being cleaned up properly, which will look like this error, will be prevented.

    0 讨论(0)
提交回复
热议问题