public virtual void MessageHandle(string topic, string message) { Logger.Debug($"Receive message on topic {topic}"); try { var eventData = _remoteEventSerializer.Deserialize<DistributedEventData>(message); var eventArgs = new DistributedEventArgs(eventData, topic, message); Trigger(this, new DistributedEventBusHandlingEvent(eventArgs));
if (!string.IsNullOrEmpty(eventData.Type)) { string pattern = @"(.*?)\[(.*?)\]"; Match match = Regex.Match(eventData.Type, pattern); if (match.Success) {
var type = match.Groups[1].Value; var type2 = match.Groups[2].Value;
var localTriggerType = typeFinder.Find(c => c.FullName == type).FirstOrDefault(); var genericType = typeFinder.Find(c => c.FullName == type2).FirstOrDefault();
if (localTriggerType != null && genericType != null) {
if (localTriggerType.GetTypeInfo().IsGenericType && localTriggerType.GetGenericArguments().Length == 1 && !genericType.IsAbstract && !genericType.IsInterface ) { var localTriggerGenericType = localTriggerType.GetGenericTypeDefinition().MakeGenericType(genericType);
if (eventData.Data.TryGetValue(PayloadKey, out var payload)) { var payloadObject = (payload as JObject).ToObject(localTriggerGenericType); Trigger(localTriggerGenericType, this, (IEventData)payloadObject);
} } }
} else { var localTriggerType = typeFinder.Find(c => c.FullName == eventData.Type).FirstOrDefault(); if (localTriggerType != null && !localTriggerType.IsAbstract && !localTriggerType.IsInterface) { if (eventData.Data.TryGetValue(PayloadKey, out var payload)) { var payloadObject = (payload as JObject).ToObject(localTriggerType); Trigger(localTriggerType, this, (IEventData)payloadObject);
}
} } Trigger(this, new DistributedEventBusHandledEvent(eventArgs));
void IEventHandler<EntityUpdatedEventData<Person>>.HandleEvent(EntityUpdatedEventData<Person> eventData) { var person = eventData.Entity; Console.WriteLine($"Remote Entity Updated - Name:{person.Name}, Age:{person.Age}, PhoneNumber:{person.PhoneNumber}"); }
void IEventHandler<EntityCreatedEventData<Person>>.HandleEvent(EntityCreatedEventData<Person> eventData) { var person = eventData.Entity; Console.WriteLine($"Remote Entity Created - Name:{person.Name}, Age:{person.Age}, PhoneNumber:{person.PhoneNumber}");
}
void IEventHandler<EntityDeletedEventData<Person>>.HandleEvent(EntityDeletedEventData<Person> eventData) { var person = eventData.Entity; Console.WriteLine($"Remote Entity Deleted - Name:{person.Name}, Age:{person.Age}, PhoneNumber:{person.PhoneNumber}");