如果人们想看看https://github.com/brandongrossutti/EventStore ,请编辑这里的 github 页面
我有一个非常奇怪的问题,无法确定根本原因。我在自己的线程上有一个 zeromq 订阅者。当它收到一条消息时,它调用一个委托,然后进入一个消息处理程序,最终得到这个动态调用
private void OnEvent(IEvent @event, bool isNew)
{
string eventName = "On" + @event.GetType().Name.Replace("Event", "");
dynamic inheritingClass = this;
MethodInfo method = inheritingClass.GetType().GetMethod(eventName);
method.Invoke(inheritingClass, new object[] { @event });
if (isNew)_uncommitedEvents.Add(@event);
}
它在调用时死了,调用堆栈和异常没有显示任何东西。如果我慢慢地通过,它似乎工作得很好。很迷茫。
提前致谢
编辑:
这是启动订阅者的线程的糟糕编写代码
private readonly IHandlerResolver _resolver;
private readonly Thread _subscriberThread;
public MessageSubscriber(OnTheWireBusConfiguration configuration, IHandlerResolver resolver)
{
_resolver = resolver;
_subscriberThread = new Thread(RecieveMessages);
_subscriberThread.Start(new object[] { configuration, resolver, new Action<Message>(ProcessMessage) });
}
private static void RecieveMessages(object o)
{
object[] obj = o as object[];
OnTheWireBusConfiguration configuration = (OnTheWireBusConfiguration)obj[0];
IHandlerResolver resolver = (IHandlerResolver)obj[1];
Action<Message> handlerDelegate = (Action<Message>) obj[2];
using (var context = new Context(configuration.MaxThreads))
{
using (Socket subscriber = context.Socket(SocketType.SUB))
{
subscriber.Subscribe("", Encoding.Unicode);
subscriber.Connect("tcp://localhost:5565");
while (true)
{
byte[] buffer = subscriber.Recv();
Message message = (Message) configuration.Deserialize(buffer);
Console.WriteLine(message);
handlerDelegate(message);
//resolver.ExecuteHandler(message);
}
}
}
}
public void ProcessMessage(Message message)
{
_resolver.ExecuteHandler(message);
}
EDIT#2 调用堆栈
GHI.EventRepository.dll!GHI.EventRepository.AggregateRoot.OnEvent(GHI.EventRepository.IEvent event, bool isNew) 第 39 行 C# GHI.EventRepository.dll!GHI.EventRepository.AggregateRoot.OnEvent(GHI.EventRepository.IEvent event) 第 29 行+ 0x12 字节 C# GHI.TestDomain.dll!GHI.TestDomain.Model.TestAggregateRoot.TestAggregateRoot(System.Guid id) 第 15 行 + 0x59 字节 C# GHI.TestDomain.dll!GHI.TestDomain.Handlers.CreateNewTestAggregateRootCommandHandler.HandleMessage(GHI.TestDomain .Messages.CreateNewTestAggregateRootCommand 消息)第 20 行 + 0x62 字节 C# [本机到托管转换]
GHI.Bus.dll!GHI.Bus.HandlerResolver.ExecuteHandler(GHI.Bus.Message message) 第 40 行 + 0x95 字节 C# GHI.Bus.ZeroMQ.dll!GHI.Bus.ZeroMQ.MessageSubscriber.ProcessMessage(GHI.Bus.Message message) 第 48 行 + 0x38 字节 C# GHI.Bus.ZeroMQ.dll!GHI.Bus.ZeroMQ.MessageSubscriber.RecieveMessages(object o) 第 39 行 + 0x13 字节 C# mscorlib.dll!System.Threading.ExecutionContext.runTryCode(object userData) + 0x173 字节
[本机到托管转换]
[托管到本机转换]
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback 回调, 对象状态, bool preserveSyncCtx) + 0xeb 字节
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback 回调,对象状态) + 0x3b 字节
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart(object obj) + 0x5d 字节[本机到托管转换]
[Appdomain 转换]
[本机到托管转换]
如果更容易的话,我可以把它放到 Github 上
编辑#4 感谢大家的帮助,很高兴我可以从那段代码中删除动态关键字。动态不是问题,这是我在堆栈中的锁定方式,再次感谢您,我已经对提到动态问题的人投了赞成票,并将接受下面的答案