我正在使用event aggregator
使用weak reference
来对待我希望处理此事件的method
对象中的subscriber
。
subscribing
成功创建weak reference
后,我的subscribers
集合会相应更新。但是,当我尝试publish
事件时,GC已清除weak reference
。以下是我的代码:
public class EventAggregator
{
private readonly ConcurrentDictionary<Type, List<Subscriber>> subscribers =
new ConcurrentDictionary<Type, List<Subscriber>>();
public void Subscribe<TMessage>(Action<TMessage> handler)
{
if (handler == null)
{
throw new ArgumentNullException("handler");
}
var messageType = typeof (TMessage);
if (this.subscribers.ContainsKey(messageType))
{
this.subscribers[messageType].Add(new Subscriber(handler));
}
else
{
this.subscribers.TryAdd(messageType, new List<Subscriber> {new Subscriber(handler)});
}
}
public void Publish(object message)
{
if (message == null)
{
throw new ArgumentNullException("message");
}
var messageType = message.GetType();
if (!this.subscribers.ContainsKey(messageType))
{
return;
}
var handlers = this.subscribers[messageType];
foreach (var handler in handlers)
{
if (!handler.IsAlive)
{
continue;
}
var actionType = handler.GetType();
var invoke = actionType.GetMethod("Invoke", new[] {messageType});
invoke.Invoke(handler, new[] {message});
}
}
private class Subscriber
{
private readonly WeakReference reference;
public Subscriber(object subscriber)
{
this.reference = new WeakReference(subscriber);
}
public bool IsAlive
{
get
{
return this.reference.IsAlive;
}
}
}
}
我subscribe
和publish
来自:
ea.Subscribe<SomeEvent>(SomeHandlerMethod);
ea.Publish(new SomeEvent { ... });
我可能正在做一些非常愚蠢的事情,说我很难看到我的错误。
答案 0 :(得分:16)
这里有一些问题(其他人已经提到过其中的一些问题),但主要的问题是编译器正在创建一个新的委托对象,没有人持有强引用。编译器需要
ea.Subscribe<SomeEvent>(SomeHandlerMethod);
并插入适当的委托转换,有效地提供:
ea.Subscribe<SomeEvent>(new Action<SomeEvent>(SomeHandlerMethod));
然后收集此代理(只有你的WeakReference
)并且订阅已被管理。
您还遇到线程安全问题(我假设您正在使用ConcurrentDictionary)。具体而言,对ConcurrentDictionary
和List
的访问根本不是线程安全的。列表需要锁定,您需要正确使用ConcurrentDictionary
进行更新。例如,在当前代码中,TryAdd
块中可能有两个单独的线程,其中一个线程将失败,导致订阅丢失。
我们可以解决这些问题,但让我概述解决方案。由于那些自动生成的委托实例,弱事件模式在.Net中实现起来可能很棘手。相反,将在Target
中捕获委托的WeakReference
,如果它有一个(如果它是静态方法,则可能不会)。然后,如果该方法是一个实例方法,我们将构造一个没有Target的等价Delegate
,因此没有强引用。
using System.Collections.Concurrent;
using System.Diagnostics;
public class EventAggregator
{
private readonly ConcurrentDictionary<Type, List<Subscriber>> subscribers =
new ConcurrentDictionary<Type, List<Subscriber>>();
public void Subscribe<TMessage>(Action<TMessage> handler)
{
if (handler == null)
throw new ArgumentNullException("handler");
var messageType = typeof(TMessage);
var handlers = this.subscribers.GetOrAdd(messageType, key => new List<Subscriber>());
lock(handlers)
{
handlers.Add(new Subscriber(handler));
}
}
public void Publish(object message)
{
if (message == null)
throw new ArgumentNullException("message");
var messageType = message.GetType();
List<Subscriber> handlers;
if (this.subscribers.TryGetValue(messageType, out handlers))
{
Subscriber[] tmpHandlers;
lock(handlers)
{
tmpHandlers = handlers.ToArray();
}
foreach (var handler in tmpHandlers)
{
if (!handler.Invoke(message))
{
lock(handlers)
{
handlers.Remove(handler);
}
}
}
}
}
private class Subscriber
{
private readonly WeakReference reference;
private readonly Delegate method;
public Subscriber(Delegate subscriber)
{
var target = subscriber.Target;
if (target != null)
{
// An instance method. Capture the target in a WeakReference.
// Construct a new delegate that does not have a target;
this.reference = new WeakReference(target);
var messageType = subscriber.Method.GetParameters()[0].ParameterType;
var delegateType = typeof(Action<,>).MakeGenericType(target.GetType(), messageType);
this.method = Delegate.CreateDelegate(delegateType, subscriber.Method);
}
else
{
// It is a static method, so there is no associated target.
// Hold a strong reference to the delegate.
this.reference = null;
this.method = subscriber;
}
Debug.Assert(this.method.Target == null, "The delegate has a strong reference to the target.");
}
public bool IsAlive
{
get
{
// If the reference is null it was a Static method
// and therefore is always "Alive".
if (this.reference == null)
return true;
return this.reference.IsAlive;
}
}
public bool Invoke(object message)
{
object target = null;
if (reference != null)
target = reference.Target;
if (!IsAlive)
return false;
if (target != null)
{
this.method.DynamicInvoke(target, message);
}
else
{
this.method.DynamicInvoke(message);
}
return true;
}
}
}
测试程序:
public class Program
{
public static void Main(string[] args)
{
var agg = new EventAggregator();
var test = new Test();
agg.Subscribe<Message>(test.Handler);
agg.Subscribe<Message>(StaticHandler);
agg.Publish(new Message() { Data = "Start test" });
GC.KeepAlive(test);
for(int i = 0; i < 10; i++)
{
byte[] b = new byte[1000000]; // allocate some memory
agg.Publish(new Message() { Data = i.ToString() });
Console.WriteLine(GC.CollectionCount(2));
GC.KeepAlive(b); // force the allocator to allocate b (if not in Debug).
}
GC.Collect();
agg.Publish(new Message() { Data = "End test" });
}
private static void StaticHandler(Message m)
{
Console.WriteLine("Static Handler: {0}", m.Data);
}
}
public class Test
{
public void Handler(Message m)
{
Console.WriteLine("Instance Handler: {0}", m.Data);
}
}
public class Message
{
public string Data { get; set; }
}
答案 1 :(得分:2)
在幕后包装SomeHandlerMethod的委托对象可能是Subscribe
和Publish
之间的垃圾收集。
尝试以下方法:
Action<SomeEvent> action = SomeHandlerMethod;
ea.Subscribe<SomeEvent>(SomeHandlerMethod);
ea.Publish(new SomeEvent { ... });
GC.KeepAlive(action);
在这种情况下,旧语法可能更清晰一点:
Action<SomeEvent> action = new Action<SomeEvent>(SomeHandlerMethod);
如果您的代码是多线程的,另外需要注意的是竞争条件,其中可能未添加订阅事件(TryAdd可能返回false)。
至于解决方案,请参阅atomaras答案:
public void Subscribe<TMessage>(IHandle<TMessage> handler)
{
[...]
public interface IHandler<T>
{
Handle(T event);
}
或者:
public void Subscribe<TMessage>(Action<TMessage> handler)
{
[...]
object targetObject = handler.Target;
MethodInfo method = handler.Method;
new Subscriber(targetObject, method)
[...]
subscriber.method.Invoke(subscriber.object, new object[]{message});
我不知道反射MethodInfo对象是否可以存储在WeakReference中,即它是否是临时的,并且如果它是强烈存储的,它是否会保留包含Type的程序集(如果我们在谈论一个dll-plugin)...
答案 2 :(得分:1)
您正在传递一个Action的实例,没有人强烈引用它,因此它可以立即用于垃圾收集。您的操作确实使用该方法对您的实例进行了强引用(如果它不是静态的)。
如果要保持相同的API签名(如果需要,还可以选择传入IHandle接口),可以执行的操作是将Subscribe参数更改为Expression,解析它并找到实例Action的目标对象,并将WeakReference保留在那里。
请参阅此处了解如何操作Action delegate. How to get the instance that call the method