问题
I have an NServiceBus configuration that is working great on developers machines and in my Development Environment.
However, when I move it to my Test Environment my messages just start getting tossed.
Here is the system:
- An app gets a TCP message from a Mainframe system and sends it to a MSMQ (call it
FromMainframe
). - An application hosted in IIS has a "Handle" method for that MSMQ and processes the messages from the mainframe.
In my Test Environment, step two only half way happens. The message is popped off the MSMQ, but not processed by my application.
Effectively my data is LOST! NServiceBus removes them from the Queue but I never get to process them. They are not even in the error queue!
These are the things I have tried in an attempt to figure out what is happening:
- Check the Config files
- Attach a remote debugger to the process to see what the
Handle
method is doing- The
Handle
method is never called (but when I attach to the Development Environment my breakpoint in myHandle
method is hit and it all works flawlessly).
- The
- Redeploy my Dev version to the Test Envioronment and try step 2 again (just in case the versions were not exactly the same.)
- Check the Config files again
- Check that the Error queue is not filling up
- The error queue stays empty (I wish it would fill up, then my data would not be LOST).
- Check for any other process that may be pulling stuff from my MSMQs
- I Turned off my IIS website and the messages in the
FromMainframe
queue start to backup. - When I turn it back on, the messages disappear fairly fast (but still not all at once). The speed that they disappear is too fast for them to be processed by my
Handle
method.
- I Turned off my IIS website and the messages in the
- Check Config files yet again.
- Run the NServiceBusTools\MsmqUtils\Runner.exe \i
- I ran it, rebooted, ran it again and again for good measure!
- Check the Configs again (I must have missed SOMETHING right?)
- Check the Development Environment Configs are not pointing to the Test Environment
- I don't think it is possible to use another computer's MSMQ as your input queue, but it does not hurt to check.
- Look for any catch blocks that could be silently killing my message.
- One last check of the Config files.
- Recreate my Test Environment on another machine (it worked flawlessly)
- Run my stuff outside of IIS.
- When I host outside of IIS (using NServiceBus.Host.exe) it all works fine. So it has to be an IIS thing right?
- Go crazy and hope that stack overflow can offer any kind of insight.
回答1:
So I know enough about what happened to throw out an "Answer".
When I setup my NServiceBus self hosting I had a call that loaded the message handlers.
NServiceBus.Configure.With().LoadMessageHandlers()
(There are more configurations, but I omitted them for brevity)
When you call this, NServiceBus scans the assmeblies for a class that implements IHandleMessages<T>
.
So, somehow, on my Test Environment Machine, the ServiceBus scan of the directory for a class that calls IHandleMessages was failing to find my class (even though the assembly was absolutely there).
Turns out that if NServiceBus does not find something that handles a message it will THROW IT AWAY!!!
This is a total design bug in my opinion. The whole idea of NServiceBus is to not lose your data, but in this case it does just that!
Now, once you know about this pitfall, there are several ways around it.
Expressly state what your handler(s) should be:
NServiceBus.Configure.With().LoadMessageHandlers<First<MyMessageType>>()
Even further protection is to add another handler that will handle "Everything else".
IMessage
is the base for all message payloads, so if you put a handler on it, it will pickup everything.
If you set IMessage to handle after your messages get handled, then it will handle everything that NServiceBus can't find a handler for. If you throw and exception in thatHandle
method that will cause NServiceBus to to move the message to theerror
queue. (What I think should be the default behavior.)
来源:https://stackoverflow.com/questions/9778954/something-making-nservicebus-lose-messages