diff --git a/src/DotNet/Reface.EventBus/DefaultCache.cs b/src/DotNet/Reface.EventBus/DefaultCache.cs index 7ca2f3e..8a61a2b 100644 --- a/src/DotNet/Reface.EventBus/DefaultCache.cs +++ b/src/DotNet/Reface.EventBus/DefaultCache.cs @@ -1,9 +1,5 @@ using System; using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Reface.EventBus { @@ -11,6 +7,11 @@ public class DefaultCache : ICache { private static readonly ConcurrentDictionary values = new ConcurrentDictionary(); + public void Clean(string name) + { + values.TryRemove(name, out object value); + } + public T GetOrCreate(string name, Func factory) { return (T)values.GetOrAdd(name, s => factory()); diff --git a/src/DotNet/Reface.EventBus/DefaultEventBus.cs b/src/DotNet/Reface.EventBus/DefaultEventBus.cs index 1091b1e..b0bf45d 100644 --- a/src/DotNet/Reface.EventBus/DefaultEventBus.cs +++ b/src/DotNet/Reface.EventBus/DefaultEventBus.cs @@ -1,6 +1,7 @@ using Reface.EventBus.EventListenerFinders; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Reflection.Emit; @@ -31,12 +32,33 @@ public DefaultEventBus() : this(new ConfigurationEventListenerFinder()) public void Publish(Event @event) { + this.Publish(@event, 0); + } + + public void Publish(Event @event, int time) + { + var allListeners = this.eventListenerFinder.CreateAllEventListeners(); allListeners = this.SortEventListeners(allListeners); Type eventType = @event.GetType(); - Dictionary infoMap = cache.GetOrCreate($"ListenerInfosOf${eventType.FullName}", + string cacheKey = $"ListenerInfosOf${eventType.FullName}"; + Dictionary infoMap = cache.GetOrCreate(cacheKey, () => CreateListenerInfos(eventType, allListeners)); + foreach (var listener in allListeners) + { + Type listenerType = listener.GetType(); + if (!infoMap.ContainsKey(listenerType)) + { + if (time != 0) + throw new KeyNotFoundException($"未发现监听器 [{listenerType.FullName}] 的信息,经清除缓存后,依然无法正常发布。"); + + Debug.WriteLine($"未发现监听器 [{listenerType.FullName}] 的信息,可能与缓存有关,清除缓存将重新发布"); + cache.Clean(cacheKey); + this.Publish(@event, time + 1); + } + } + foreach (var listener in allListeners) { Type listenerType = listener.GetType(); @@ -50,6 +72,8 @@ public void Publish(Event @event) } } + + /// /// Sort all EventListeners b Priority /// @@ -71,7 +95,7 @@ private IEnumerable SortEventListeners(IEnumerable x.L) .ToList(); } - + private Dictionary CreateListenerInfos(Type eventType, IEnumerable allListeners) { Dictionary result = new Dictionary(); diff --git a/src/DotNet/Reface.EventBus/ICache.cs b/src/DotNet/Reface.EventBus/ICache.cs index 42eb667..7fd0fd3 100644 --- a/src/DotNet/Reface.EventBus/ICache.cs +++ b/src/DotNet/Reface.EventBus/ICache.cs @@ -8,5 +8,7 @@ namespace Reface.EventBus public interface ICache { T GetOrCreate(String name, Func factory); + + void Clean(string name); } } diff --git a/src/DotNet/Reface.EventBus/package.nuspec b/src/DotNet/Reface.EventBus/package.nuspec index 435c8d7..cd63fb2 100644 --- a/src/DotNet/Reface.EventBus/package.nuspec +++ b/src/DotNet/Reface.EventBus/package.nuspec @@ -4,11 +4,11 @@ Reface.EventBus - 3.2.0 + 3.2.3 Reface.EventBus - ShimizuShiori - ShimizuShiori + Felix + Felix https://github.com/ShimizuShiori/EventBus/blob/master/LICENSE https://github.com/ShimizuShiori/EventBus EventBus in C# - - + 添加验证,事件源不能为 null - + 当发布消息时,才获取所有事件监听者 - Copyright © HP Inc. 2019