Beispiel #1
0
        protected override void OnRequestMessageReceived(object sender, DuplexChannelMessageEventArgs e)
        {
            using (EneterTrace.Entering())
            {
                if (MessageReceived == null)
                {
                    EneterTrace.Warning(TracedObject + ErrorHandler.NobodySubscribedForMessage);
                    return;
                }

                TypedRequestReceivedEventArgs <_RequestType> aRequestReceivedEventArgs = null;

                try
                {
                    _RequestType aRequestMessage = mySerializer.ForResponseReceiver(e.ResponseReceiverId).Deserialize <_RequestType>(e.Message);
                    aRequestReceivedEventArgs = new TypedRequestReceivedEventArgs <_RequestType>(e.ResponseReceiverId, e.SenderAddress, aRequestMessage);
                }
                catch (Exception err)
                {
                    EneterTrace.Warning(TracedObject + "failed to deserialize the request message.", err);
                    aRequestReceivedEventArgs = new TypedRequestReceivedEventArgs <_RequestType>(e.ResponseReceiverId, e.SenderAddress, err);
                }

                try
                {
                    MessageReceived(this, aRequestReceivedEventArgs);
                }
                catch (Exception err)
                {
                    EneterTrace.Warning(TracedObject + ErrorHandler.DetectedException, err);
                }
            }
        }
        private void OnRequestMessageReceived(object sender, TypedRequestReceivedEventArgs <MultiTypedMessage> e)
        {
            using (EneterTrace.Entering())
            {
                if (e.ReceivingError != null)
                {
                    EneterTrace.Warning(TracedObject + ErrorHandler.FailedToReceiveMessage, e.ReceivingError);
                }
                else
                {
                    TMessageHandler aMessageHandler;

                    using (ThreadLock.Lock(myMessageHandlers))
                    {
                        myMessageHandlers.TryGetValue(e.RequestMessage.TypeName, out aMessageHandler);
                    }

                    if (aMessageHandler != null)
                    {
                        object aMessageData;
                        try
                        {
                            aMessageData = mySerializer.ForResponseReceiver(e.ResponseReceiverId).Deserialize(aMessageHandler.Type, e.RequestMessage.MessageData);

                            try
                            {
                                aMessageHandler.Invoke(e.ResponseReceiverId, e.SenderAddress, aMessageData, null);
                            }
                            catch (Exception err)
                            {
                                EneterTrace.Warning(TracedObject + ErrorHandler.DetectedException, err);
                            }
                        }
                        catch (Exception err)
                        {
                            try
                            {
                                aMessageHandler.Invoke(e.ResponseReceiverId, e.SenderAddress, null, err);
                            }
                            catch (Exception err2)
                            {
                                EneterTrace.Warning(TracedObject + ErrorHandler.DetectedException, err2);
                            }
                        }
                    }
                    else
                    {
                        EneterTrace.Warning(TracedObject + ErrorHandler.NobodySubscribedForMessage + " Message type = " + e.RequestMessage.TypeName);
                    }
                }
            }
        }
        public void RegisterRequestMessageReceiver <T>(EventHandler <TypedRequestReceivedEventArgs <T> > handler)
        {
            using (EneterTrace.Entering())
            {
                if (handler == null)
                {
                    string anError = TracedObject + "failed to register handler for message " + typeof(T).Name + " because the input parameter handler is null.";
                    EneterTrace.Error(anError);
                    throw new ArgumentNullException(anError);
                }

                using (ThreadLock.Lock(myMessageHandlers))
                {
                    TMessageHandler aMessageHandler;
                    myMessageHandlers.TryGetValue(typeof(T).Name, out aMessageHandler);

                    if (aMessageHandler != null)
                    {
                        string anError = TracedObject + "failed to register handler for message " + typeof(T).Name + " because the handler for such class name is already registered.";
                        EneterTrace.Error(anError);
                        throw new InvalidOperationException(anError);
                    }

                    // Note: the invoking method must be cached for particular types because
                    //       during deserialization the generic argument is not available and so it would not be possible
                    //       to instantiate TypedRequestReceivedEventArgs<T>.
                    Action <string, string, object, Exception> anEventInvoker = (responseReceiverId, senderAddress, message, receivingError) =>
                    {
                        TypedRequestReceivedEventArgs <T> anEvent;
                        if (receivingError == null)
                        {
                            anEvent = new TypedRequestReceivedEventArgs <T>(responseReceiverId, senderAddress, (T)message);
                        }
                        else
                        {
                            anEvent = new TypedRequestReceivedEventArgs <T>(responseReceiverId, senderAddress, receivingError);
                        }
                        handler(this, anEvent);
                    };
                    myMessageHandlers[typeof(T).Name] = new TMessageHandler(typeof(T), anEventInvoker);
                }
            }
        }