public bool OnPublish(byte[] byteStream, TypeDescriptor typeDescriptor) { logger.Debug(string.Format("Acquired message of type {0}", typeDescriptor)); try { // try recreating this type object sendObject; Type type; MessageSerializer.UnPackData(typeDescriptor, byteStream, out sendObject, out type); // invoke this generic method with type t // TODO: this is totaly not refactor aware use expression tree to get this publish thing MethodInfo methodInfo = _localEventAggregator.GetType().GetMethod("Publish"); MethodInfo goodMethodInfo = methodInfo.MakeGenericMethod(type); goodMethodInfo.Invoke(_localEventAggregator, new[] { sendObject }); } catch (Exception e) { logger.Warn("The type not be recreated", e); } // NOTE: this is the role of subsystem to answer return(true); }
private EventBase GetEvent(Type payloadData) { var eventType = typeof(PubSubEvent <>).MakeGenericType(payloadData); var methodInfo = _eventAggregator.GetType().GetMethod(nameof(IEventAggregator.GetEvent)); return(methodInfo.MakeGenericMethod(eventType).Invoke(_eventAggregator, new object[0]) as EventBase); }
/// <summary> /// Gets the <see cref="IEventAggregator"/>s Subscribe or Unsubscribe method for the specific /// <see cref="handlerType"/>. /// </summary> /// <param name="eventAggregator"> /// The <see cref="IEventAggregator"/>. /// </param> /// <param name="methodName"> /// The name of the method to search for. /// </param> /// <param name="handlerType"> /// The <see cref="Type"/> of the <see cref="IEvent"/> handler. /// </param> /// <returns> /// The <see cref="MethodInfo"/> for the subscribe or unsubscribe method. /// </returns> private static MethodInfo GetSubscriptionMethod( IEventAggregator eventAggregator, string methodName, Type handlerType) { // Get the type of IEvent we're dealing with Type eventType = handlerType.GenericTypeArguments.First(); // Get all possible methods we could have MethodInfo[] possibleMethods = eventAggregator.GetType().GetMethods().Where(m => m.Name == methodName).ToArray(); foreach (MethodInfo method in possibleMethods) { // Make sure amount of generic parameters in handler match Type[] delegateParameters = method.GetParameters().First().ParameterType.GenericTypeArguments; if (delegateParameters.Length != handlerType.GetGenericArguments().Length) { continue; } // Make sure the first parameter is generic Type firstParameter = delegateParameters.First(); if (!firstParameter.IsGenericParameter) { continue; } // Found our method return(method.MakeGenericMethod(eventType)); } // Nothing found throw new Exception($"Couldn't find any method named {methodName} which deals with methods with parameters {string.Join(", ", handlerType.GenericTypeArguments.Select(t => t.Name))}."); }
public EventBus(IEventAggregator aggregator) { this.aggregator = aggregator; publishMethod = (from m in aggregator.GetType().GetMethods() let parameters = m.GetParameters() let methodName = m.Name where methodName == "Publish" && parameters != null && parameters.Length == 1 select m).First(); }
public bool RecieveSingle(IEventAggregator eventAggregator, object sendObject, Type type) { // TODO: provide lambda expression reader from some trick in Moku MethodInfo methodInfo = eventAggregator.GetType().GetMethod("PublishSingleDelivery"); MethodInfo goodMethodInfo = methodInfo.MakeGenericMethod(type); // semantic is not used for local event aggregator object result = goodMethodInfo.Invoke(eventAggregator, new[] { sendObject, null }); return((bool)result); }
public EventBus(IEventAggregator aggregator) { this._aggregator = aggregator; //获得EventAggregator中的Handle方法 _handleMethod = (from m in aggregator.GetType().GetMethods() let parameters = m.GetParameters() let methodName = m.Name where methodName == "Handle" && parameters != null && parameters.Length == 1 select m).First(); }
public void TryDeliverBufferedMessages(IEventAggregator localEA) { var deliveredMessages = new List <BufferedBinaryMessage>(); foreach (var bufferedBinaryMessage in _bufferedBinaryMessages) { // omit already expired messages if (bufferedBinaryMessage.ExpiryTime <= DateTime.Now) { continue; } // try deserialize object message; Type type; try { MessageSerializer.UnPackData(bufferedBinaryMessage.Descriptor, bufferedBinaryMessage.Message, out message, out type); } catch { // binary message is yet not deliverable continue; } // message is deliverable // invoke this generic method with type T MethodInfo methodInfo = localEA.GetType().GetMethod("PublishTimelyBuffered"); MethodInfo goodMethodInfo = methodInfo.MakeGenericMethod(type); goodMethodInfo.Invoke(localEA, new[] { message, bufferedBinaryMessage.ExpiryTime }); // add to delivered deliveredMessages.Add(bufferedBinaryMessage); } // clean the buffer from delivered messages foreach (var deliveredMessage in deliveredMessages) { _bufferedBinaryMessages.Remove(deliveredMessage); } // clean binary buffer from expired messages _bufferedBinaryMessages.RemoveAll(m => m.ExpiryTime <= DateTime.Now); }
public void TryDeliverBufferedMessages(IEventAggregator localEA) { var deliveredMessages = new List<BufferedBinaryMessage>(); foreach (var bufferedBinaryMessage in _bufferedBinaryMessages) { // omit already expired messages if (bufferedBinaryMessage.ExpiryTime <= DateTime.Now) continue; // try deserialize object message; Type type; try { MessageSerializer.UnPackData(bufferedBinaryMessage.Descriptor, bufferedBinaryMessage.Message, out message, out type); } catch { // binary message is yet not deliverable continue; } // message is deliverable // invoke this generic method with type T MethodInfo methodInfo = localEA.GetType().GetMethod("PublishTimelyBuffered"); MethodInfo goodMethodInfo = methodInfo.MakeGenericMethod(type); goodMethodInfo.Invoke(localEA, new[] {message, bufferedBinaryMessage.ExpiryTime}); // add to delivered deliveredMessages.Add(bufferedBinaryMessage); } // clean the buffer from delivered messages foreach (var deliveredMessage in deliveredMessages) { _bufferedBinaryMessages.Remove(deliveredMessage); } // clean binary buffer from expired messages _bufferedBinaryMessages.RemoveAll(m => m.ExpiryTime <= DateTime.Now); }
public MsmqEventBus(string path) { this._aggregator = ServiceLocator.Instance.GetService <IEventAggregator>(); _publishMethod = (from m in _aggregator.GetType().GetMethods() let parameters = m.GetParameters() let methodName = m.Name where methodName == "Handle" && parameters != null && parameters.Length == 1 select m).First(); var options = new MsmqBusOptions(path); // 初始化消息队列对象 // 更多信息参考:https://msdn.microsoft.com/zh-cn/library/System.Messaging.MessageQueue(v=vs.110).aspx this._messageQueue = new MessageQueue(path, options.SharedModeDenyReceive, options.EnableCache, options.QueueAccessMode); this._useInternalTransaction = options.UseInternalTransaction && _messageQueue.Transactional; }