示例#1
0
        static void ProcessResponse(IAsyncResult result)
        {
            MessageSession  session = result.AsyncState as MessageSession;
            BrokeredMessage message = session.EndReceive(result);

            if (message == null)
            {
                Console.WriteLine("ERROR: Message Receive Timeout.");
            }
            else
            {
                SampleManager.OutputMessageInfo("RESPONSE: ", message);
            }
        }
示例#2
0
        private void OnMessage(IAsyncResult ar)
        {
            BrokeredMessage msg;

            try
            {
                msg = _session.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)
            {
                _logger.Write(LogLevel.Error, "Failed to EndReceive in " + _sessionId, exception);
                BusFailed(this, new ExceptionEventArgs(exception));
                return;
            }

            Guid         requestId;
            ITaskHandler taskHandler;

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

                if (!Guid.TryParse(requestIdStr, out requestId))
                {
                    throw new UnknownMessageException("Failed to parse 'ReplyTo' as a Guid for msg '" +
                                                      msg.ToString() +
                                                      "'. Does something else than this library use the configured queue?");
                }

                if (!_queue.TryRemove(requestId, out taskHandler))
                {
                    throw new UnknownMessageException("Failed to load task handler for request '" + requestId +
                                                      "'. Maybe it have been removed due to the timeout time?");
                }
            }
            catch (Exception exception)
            {
                BusFailed(this, new ExceptionEventArgs(exception));
                ReceiveMessage();
                return;
            }


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

                    ReceiveMessage();
                    return;
                }

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


                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 queryResponse = genMethod.Invoke(Serializer.Serializer.Instance, new object[] { msg });
                if (queryResponse is Exception)
                {
                    taskHandler.SetException((Exception)queryResponse);
                }
                else
                {
                    taskHandler.SetResult(queryResponse);
                }
            }
            catch (Exception exception)
            {
                BusFailed(this, new ExceptionEventArgs(exception));
            }


            ReceiveMessage();
        }