public void SendMessage(string message)
 {
     lock (this)
     {
         AttachedDuplexOutputChannel.SendMessage(message);
     }
 }
Пример #2
0
        public virtual void AttachDuplexOutputChannel(IDuplexOutputChannel duplexOutputChannel)
        {
            using (EneterTrace.Entering())
            {
                using (ThreadLock.Lock(myDuplexOutputChannelManipulatorLock))
                {
                    Attach(duplexOutputChannel);

                    try
                    {
                        AttachedDuplexOutputChannel.OpenConnection();
                    }
                    catch (Exception err)
                    {
                        try
                        {
                            DetachDuplexOutputChannel();
                        }
                        catch
                        {
                            // Ignore exception in exception.
                        }

                        string aMessage = TracedObject + ErrorHandler.FailedToOpenConnection;
                        EneterTrace.Error(aMessage, err);
                        throw;
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// The method is called when a message from the attached duplex input channel is received.
        /// The received message is wrapped and sent to the duplex output channel.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnMessageReceived(object sender, DuplexChannelMessageEventArgs e)
        {
            using (EneterTrace.Entering())
            {
                if (!IsDuplexOutputChannelAttached)
                {
                    EneterTrace.Error(TracedObject + "is not attached to the duplex output channel.");
                    return;
                }

                try
                {
                    using (ThreadLock.Lock(myDuplexInputChannels))
                    {
                        myDuplexInputChannels[e.ChannelId].ResponseReceiverId = e.ResponseReceiverId;
                    }

                    ISerializer aSerializer = mySerializer.ForResponseReceiver(e.ResponseReceiverId);
                    object      aMessage    = DataWrapper.Wrap(e.ChannelId, e.Message, aSerializer);
                    AttachedDuplexOutputChannel.SendMessage(aMessage);
                }
                catch (Exception err)
                {
                    EneterTrace.Error(TracedObject + "failed to send the message to the duplex output channel '" + e.ChannelId + "'.", err);
                }
            }
        }
Пример #4
0
 public virtual void DetachDuplexOutputChannel()
 {
     using (EneterTrace.Entering())
     {
         using (ThreadLock.Lock(myDuplexOutputChannelManipulatorLock))
         {
             if (AttachedDuplexOutputChannel != null)
             {
                 try
                 {
                     // Try to notify, the connection is closed.
                     AttachedDuplexOutputChannel.CloseConnection();
                 }
                 finally
                 {
                     // Detach the event handler.
                     AttachedDuplexOutputChannel.ConnectionOpened        -= OnConnectionOpened;
                     AttachedDuplexOutputChannel.ConnectionClosed        -= OnConnectionClosed;
                     AttachedDuplexOutputChannel.ResponseMessageReceived -= OnResponseMessageReceived;
                     AttachedDuplexOutputChannel = null;
                 }
             }
         }
     }
 }
        public void AttachDuplexOutputChannel(IDuplexOutputChannel duplexOutputChannel)
        {
            lock (this)
            {
                Attach(duplexOutputChannel);

                AttachedDuplexOutputChannel.OpenConnection();
            }
        }
 public void DetachDuplexOutputChannel()
 {
     lock (this)
     {
         if (AttachedDuplexOutputChannel != null)
         {
             AttachedDuplexOutputChannel.CloseConnection();
             AttachedDuplexOutputChannel.ResponseMessageReceived -= OnResponseMessageReceived;
             AttachedDuplexOutputChannel = null;
         }
     }
 }
Пример #7
0
        private object CallService(RpcMessage rpcRequest)
        {
            using (EneterTrace.Entering())
            {
                if (AttachedDuplexOutputChannel == null)
                {
                    string anError = TracedObject + ErrorHandler.FailedToSendMessageBecauseNotAttached;
                    EneterTrace.Error(anError);
                    throw new InvalidOperationException(anError);
                }

                try
                {
                    RemoteCallContext anRpcSyncContext = new RemoteCallContext();
                    using (ThreadLock.Lock(myPendingRemoteCalls))
                    {
                        myPendingRemoteCalls.Add(rpcRequest.Id, anRpcSyncContext);
                    }

                    // Send the request.
                    object aSerializedMessage = mySerializer.ForResponseReceiver(AttachedDuplexOutputChannel.ResponseReceiverId).Serialize <RpcMessage>(rpcRequest);
                    AttachedDuplexOutputChannel.SendMessage(aSerializedMessage);

                    // Wait for the response.
                    if (!anRpcSyncContext.RpcCompleted.WaitOne((int)myRpcTimeout.TotalMilliseconds))
                    {
                        throw new TimeoutException("Remote call has not returned within the specified timeout " + myRpcTimeout + ".");
                    }

                    if (anRpcSyncContext.Error != null)
                    {
                        throw anRpcSyncContext.Error;
                    }

                    return(anRpcSyncContext.SerializedReturnValue);
                }
                catch (Exception err)
                {
                    EneterTrace.Error(TracedObject + "." + rpcRequest.OperationName + "(..) " + ErrorHandler.FailedToSendMessage, err);
                    throw;
                }
                finally
                {
                    using (ThreadLock.Lock(myPendingRemoteCalls))
                    {
                        myPendingRemoteCalls.Remove(rpcRequest.Id);
                    }
                }
            }
        }
        public void SendMessage(string message)
        {
            using (EneterTrace.Entering())
            {
                if (AttachedDuplexOutputChannel == null)
                {
                    string anError = TracedObject + ErrorHandler.FailedToSendMessageBecauseNotAttached;
                    EneterTrace.Error(anError);
                    throw new InvalidOperationException(anError);
                }

                try
                {
                    AttachedDuplexOutputChannel.SendMessage(message);
                }
                catch (Exception err)
                {
                    EneterTrace.Error(TracedObject + ErrorHandler.FailedToSendMessage, err);
                    throw;
                }
            }
        }
Пример #9
0
        public void SendRequestMessage(_RequestType message)
        {
            using (EneterTrace.Entering())
            {
                if (AttachedDuplexOutputChannel == null)
                {
                    string anError = TracedObject + ErrorHandler.FailedToSendMessageBecauseNotAttached;
                    EneterTrace.Error(anError);
                    throw new InvalidOperationException(anError);
                }

                try
                {
                    object aRequestMessage = mySerializer.ForResponseReceiver(AttachedDuplexOutputChannel.ResponseReceiverId).Serialize <_RequestType>(message);
                    AttachedDuplexOutputChannel.SendMessage(aRequestMessage);
                }
                catch (Exception err)
                {
                    EneterTrace.Error(TracedObject + ErrorHandler.FailedToSendMessage, err);
                    throw;
                }
            }
        }
Пример #10
0
        private void Send(BrokerMessage message)
        {
            using (EneterTrace.Entering())
            {
                if (AttachedDuplexOutputChannel == null)
                {
                    string anError = TracedObject + "failed to send the message because it is not attached to any duplex output channel.";
                    EneterTrace.Error(anError);
                    throw new InvalidOperationException(anError);
                }

                try
                {
                    object aSerializedMessage = mySerializer.ForResponseReceiver(AttachedDuplexOutputChannel.ResponseReceiverId).Serialize <BrokerMessage>(message);
                    AttachedDuplexOutputChannel.SendMessage(aSerializedMessage);
                }
                catch (Exception err)
                {
                    string anError = TracedObject + "failed to send a message to the Broker.";
                    EneterTrace.Error(anError, err);
                    throw;
                }
            }
        }