/// <summary> /// Method is called when a response is received from the duplex output channel. /// It wrapps the response and sends the wrapped response to the correct response receiver as the response. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnResponseMessageReceived(object sender, DuplexChannelMessageEventArgs e) { using (EneterTrace.Entering()) { try { // try to find the response receiver id where the wrapped message should be responded. TDuplexConnection aConnction = null; using (ThreadLock.Lock(myConnections)) { aConnction = myConnections.FirstOrDefault(x => x.DuplexOutputChannel == (IDuplexOutputChannel)sender); } if (aConnction != null) { ISerializer aSerializer = mySerializer.ForResponseReceiver(aConnction.ResponseReceiverId); object aMessage = DataWrapper.Wrap(e.ChannelId, e.Message, aSerializer); AttachedDuplexInputChannel.SendResponseMessage(aConnction.ResponseReceiverId, aMessage); } else { EneterTrace.Warning(TracedObject + "failed to send the response message because the response receiver id does not exist. It is possible the response receiver has already been disconnected."); } } catch (Exception err) { EneterTrace.Error(TracedObject + ErrorHandler.FailedToSendResponseMessage, err); } } }
private IEnumerable <string> Send(string responseReceiverId, object serializedMessage) { using (EneterTrace.Entering()) { if (AttachedDuplexInputChannel == null) { string anErrorMessage = TracedObject + "failed to send the message because the it is not attached to duplex input channel."; EneterTrace.Error(anErrorMessage); throw new InvalidOperationException(anErrorMessage); } try { AttachedDuplexInputChannel.SendResponseMessage(responseReceiverId, serializedMessage); } catch (Exception err) { EneterTrace.Error(TracedObject + "failed to send the message. The client will be disconnected and unsubscribed from all messages.", err); try { // Try to disconnect the client. AttachedDuplexInputChannel.DisconnectResponseReceiver(responseReceiverId); } catch { } // Unsubscribe the failed client. return(Unsubscribe(responseReceiverId, null)); } return(new string[0]); } }
/// <summary> /// It is called when a response message from the service is received. /// The response message must be redirected to the associated client. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnOutputChannelResponseMessageReceived(object sender, DuplexChannelMessageEventArgs e) { using (EneterTrace.Entering()) { string aResponseReceiverId = null; using (ThreadLock.Lock(myConnectionsLock)) { TConnection aConnection = myOpenConnections.FirstOrDefault(x => x.DuplexOutputChannel.ResponseReceiverId == e.ResponseReceiverId); if (aConnection != null) { aResponseReceiverId = aConnection.ResponseReceiverId; } } if (aResponseReceiverId == null) { EneterTrace.Warning(TracedObject + "could not find receiver for the incoming response message."); return; } using (ThreadLock.Lock(myDuplexInputChannelManipulatorLock)) { // Send the response message via the duplex input channel to the sender. if (AttachedDuplexInputChannel != null) { try { AttachedDuplexInputChannel.SendResponseMessage(aResponseReceiverId, e.Message); } catch (Exception err) { EneterTrace.Error(TracedObject + ErrorHandler.FailedToSendResponseMessage, err); } } else { EneterTrace.Error(TracedObject + "cannot send the response message when the duplex input channel is not attached."); } } } }
public void SendResponseMessage(string responseReceiverId, string responseMessage) { using (EneterTrace.Entering()) { if (AttachedDuplexInputChannel == null) { string anError = TracedObject + "failed to send the response message because it is not attached to any duplex input channel."; EneterTrace.Error(anError); throw new InvalidOperationException(anError); } try { AttachedDuplexInputChannel.SendResponseMessage(responseReceiverId, responseMessage); } catch (Exception err) { EneterTrace.Error(TracedObject + ErrorHandler.FailedToSendResponseMessage, err); throw; } } }