/// <summary> /// Attempts to create a communication connection. /// </summary> /// <param name="exception">Returns an <see cref="Exception"/> if anything should go wrong in the attempt.</param> /// <returns>The state of the channel's connection.</returns> protected override bool OnOpen(out Exception exception) { exception = null; bool result = false; if (_Factory == null) { try { _ClientActual = new ClientActual(base.ClientConfiguration, base.TransportController, new Action <MessageEventArgs>((e) => { // OnSendMessage base.SendMessageOnMessageBus(e); }), new Action(() => { // OnCloseClient Task.Run(() => { base.Close(out Exception ex); }); }), new Action <int, TimeSpan>((retryCount, retryDuration) => { // OnRestartClient Task.Run(() => { // close client base.Close(out Exception exClose); // attempt to restart client for (int i = 0; i < retryCount; i++) { Threading.ThreadingHelper.Sleep(retryDuration); var openState = base.Open(out Exception exState); if (openState == ChannelStates.Open) { break; } } }); })); _Factory = new System.ServiceModel.DuplexChannelFactory <IService>(new System.ServiceModel.InstanceContext(_ClientActual), Binding, new System.ServiceModel.EndpointAddress(AddressUri)); _ServiceProxy = _Factory.CreateChannel(); result = true; } catch (Exception ex) { DestroyConnection(); exception = new Exception("Unable to establish connection with server at " + Address, ex); } } return(result); }
private void DestroyConnection() { if (_Factory != null) { try { _Factory.Close(); } catch { _Factory.Abort(); } ((IDisposable)_Factory).Dispose(); _Factory = null; } _ServiceProxy = null; _ClientActual = null; }