private async Task Execute <T>(BrokeredMessage msg) where T : ApplicationEvent { var appEvent = Serializer.Serializer.Instance.Deserialize <T>(msg); _logger.Write(LogLevel.Debug, "Received event: " + DebugSerializer.Serialize(appEvent)); using (var scope = _container.CreateChildContainer()) { var handlers = ( from handler in scope.ResolveAll <IApplicationEventSubscriber <T> >() select handler.HandleAsync(appEvent) ).ToArray(); await Task.WhenAll(handlers); SuccessTask(scope); } }
protected void SendReply(string sessionId, Guid requestId, object reply) { var msg = Serializer.Serializer.Instance.Serialize(reply); _logger.Write(LogLevel.Info, "Request " + requestId + ", Session " + sessionId + ", Reply: " + DebugSerializer.Serialize(reply)); msg.ReplyTo = requestId.ToString(); msg.SessionId = sessionId; msg.Properties[MessageProperties.PayloadTypeName] = reply.GetType().AssemblyQualifiedName; _resultQueueClient.Send(msg); }
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(); }