示例#1
0
        public static void OnMessageReceive(IAsyncResult result)
        {
            QueueClient queueClient = (QueueClient)result.AsyncState;

            try
            {
                //Receive the message with the EndReceive call
                BrokeredMessage message = queueClient.EndReceive(result);

                if (message != null)
                {
                    Console.WriteLine("\nMessage Received: Id = {0}, Body = {1}", message.MessageId, message.GetBody <string>());

                    // The following is necessary in PeekLock Receive mode only.
                    // Complete the message asynchronously.
                    message.BeginComplete(OnMessageComplete, message);
                }

                else
                {
                    Console.WriteLine("OnMessageReceive: Unexpected Error, Did not receive any messages");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("OnMessageReceive: Unexpected exception in {0}", e.ToString());
                throw;
            }
        }
        private void OnMessage(IAsyncResult ar)
        {
            BrokeredMessage brokeredMessage = null;
            try
            {
                brokeredMessage = _queueClient.EndReceive(ar);
                if (brokeredMessage == null)
                {
                    _logger.Write(LogLevel.Debug, "Received null message (i.e. the Azure ServiceBus library completed the async op without receiving a message)");
                    ReceiveMessage();
                    return;
                }

                var typeName = (string) brokeredMessage.Properties[MessageProperties.PayloadTypeName];
                if (!CheckTypeName(typeName, brokeredMessage))
                {
                    ReceiveMessage();
                    return;
                }

                var type = Type.GetType(typeName, false);
                if (!CheckMessageType(type, brokeredMessage, typeName))
                {
                    ReceiveMessage();
                    return;
                }
                    

                var method = _genericMethod.MakeGenericMethod(type);
                method.Invoke(this, new object[] {brokeredMessage});

                brokeredMessage.Complete();
            }
            catch (Exception exception)
            {
                _logger.Write(LogLevel.Error, "Failed to process " + brokeredMessage, exception);

                var targetInvoke = exception as TargetInvocationException;
                if (targetInvoke != null)
                {
                    exception = exception.InnerException;
                }

                var e = new BusMessageErrorEventArgs(brokeredMessage, exception);
                CommandBusFailed(this, e);
                if (brokeredMessage != null)
                {
                    if (e.MessageTask == MessageHandling.RemoveMessage)
                    {
                        brokeredMessage.Complete();
                    }
                    else
                    {
                        brokeredMessage.Abandon();
                    }
                }
            }

            ReceiveMessage();
        }
示例#3
0
        private void OnMessage(IAsyncResult ar)
        {
            BrokeredMessage msg;

            try
            {
                msg = _requestQueue.EndReceive(ar);
                if (msg == null)
                {
                    _logger.Write(LogLevel.Info, "Received empty message.");
                    ReceiveMessage();
                    return;
                }
            }
            catch (Exception exception)
            {
                BusFailed(this, new ExceptionEventArgs(exception));
                ReceiveMessage();
                return;
            }

            Guid queryId;

            try
            {
                var queryIdStr = msg.MessageId;
                if (queryIdStr == null)
                {
                    throw new UnknownMessageException("Did not find the 'MessageId' for the broker message '" +
                                                      msg.ToString() +
                                                      "'. Does something else than this library use the configured queue?");
                }

                if (!Guid.TryParse(queryIdStr, out queryId))
                {
                    throw new UnknownMessageException("Failed to parse 'MessageId' as a Guid for msg '" +
                                                      msg.ToString() +
                                                      "'. Does something else than this library use the configured queue?");
                }
            }
            catch (Exception exception)
            {
                _logger.Write(LogLevel.Error, "Failed to find QueryId for " + msg, exception);
                BusFailed(this, new ExceptionEventArgs(exception));
                ReceiveMessage();
                return;
            }

            try
            {
                var typeName = (string)msg.Properties[MessageProperties.PayloadTypeName];
                if (typeName == null)
                {
                    throw new UnknownMessageException(
                              "Did not find the 'PayloadTypeName' property in the broker message for query '" +
                              queryId + "'. Does something else than this library use the configured queue?");
                }

                var type = Type.GetType(typeName, false);
                if (type == null)
                {
                    throw new UnknownMessageException("Failed to load type '" + typeName +
                                                      "'. Have all assemblies been loaded?");
                }


                if (_serializerMethod == null)
                {
                    _serializerMethod = Serializer.Serializer.Instance.GetType().GetMethod("Deserialize");
                    if (_serializerMethod == null)
                    {
                        throw new UnknownMessageException(
                                  "Failed to identify the Deserialize method in Serializer.Instance. Strange.");
                    }
                }

                var genMethod = _serializerMethod.MakeGenericMethod(type);
                var query     = genMethod.Invoke(Serializer.Serializer.Instance, new object[] { msg });


                var method   = _queryHandlerMethod.MakeGenericMethod(type, type.BaseType.GenericTypeArguments[0]);
                var response = method.Invoke(this, new object[] { query });
                Reply(msg.ReplyToSessionId, queryId, response);
                msg.Complete();
                _logger.Write(LogLevel.Info, "Replied to " + msg.MessageId);
            }
            catch (Exception exception)
            {
                _logger.Write(LogLevel.Error, "Failed to process " + msg, exception);
                Reply(msg.ReplyToSessionId, queryId, exception);
            }


            ReceiveMessage();
        }
示例#4
0
        private void OnMessage(IAsyncResult ar)
        {
            BrokeredMessage msg;

            try
            {
                msg = _queryQueueClient.EndReceive(ar);
                if (msg == null)
                {
                    _logger.Write(LogLevel.Debug, "Received null message (i.e. the Azure ServiceBus library completed the async op without receiving a message)");
                    ReceiveMessage();
                    return;
                }
            }
            catch (Exception exception)
            {
                BusFailed(this, new ExceptionEventArgs(exception));
                ReceiveMessage();
                return;
            }

            Guid requestId;

            try
            {
                var queryIdStr = msg.MessageId;
                if (queryIdStr == null)
                {
                    throw new UnknownMessageException("Did not find the 'MessageId' for the broker message '" +
                                                      msg.ToString() +
                                                      "'. Does something else than this library use the configured queue?");
                }

                if (!Guid.TryParse(queryIdStr, out requestId))
                {
                    throw new UnknownMessageException("Failed to parse 'MessageId' as a Guid for msg '" +
                                                      msg.ToString() +
                                                      "'. Does something else than this library use the configured queue?");
                }
            }
            catch (Exception exception)
            {
                BusFailed(this, new ExceptionEventArgs(exception));
                ReceiveMessage();
                return;
            }

            try
            {
                var typeName = (string)msg.Properties[MessageProperties.PayloadTypeName];
                if (typeName == null)
                {
                    throw new UnknownMessageException(
                              "Did not find the 'PayloadTypeName' property in the broker message for query '" +
                              requestId + "'. Does something else than this library use the configured queue?");
                }

                var type = Type.GetType(typeName, false);
                if (type == null)
                {
                    var ex = new UnknownMessageException("Failed to load type '" + typeName +
                                                         "'. Have all assemblies been loaded?");
                    BusFailed(this, new ExceptionEventArgs(ex));
                    throw ex;
                }


                if (_serializerMethod == null)
                {
                    _serializerMethod = Serializer.Serializer.Instance.GetType().GetMethod("Deserialize");
                    if (_serializerMethod == null)
                    {
                        var ex = new UnknownMessageException(
                            "Failed to identify the Deserialize method in Serializer.Instance. Strange.");
                        BusFailed(this, new ExceptionEventArgs(ex));
                        throw ex;
                    }
                }

                var genMethod = _serializerMethod.MakeGenericMethod(type);
                var request   = genMethod.Invoke(Serializer.Serializer.Instance, new object[] { msg });

                _logger.Write(LogLevel.Debug, "Received request: " + DebugSerializer.Serialize(request));

                var method = _requestHandlerMethod.MakeGenericMethod(type, type.BaseType.GenericTypeArguments[0]);
                var reply  = method.Invoke(this, new object[] { request });
                SendReply(msg.ReplyToSessionId, requestId, reply);
            }
            catch (Exception exception)
            {
                _logger.Write(LogLevel.Warning, "Failed to process request " + requestId, exception);
                if (exception is TargetInvocationException)
                {
                    exception = exception.InnerException;
                }
                if (exception is AggregateException && ((AggregateException)exception).InnerExceptions.Count == 1)
                {
                    exception = exception.InnerException;
                }

                SendReply(msg.ReplyToSessionId, requestId, exception);
            }


            ReceiveMessage();
        }