Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to throw a MessageQueueException?

I am using a mock object in RhinoMocks to represent a class that makes calls to MessageQueue.GetPublicQueues. I want to simulate the exception thrown when message queueing is operating in workgroup mode, which is a MessageQueueException, to ensure that I am catching the exception correctly

The MessageQueueException has no public constructor, only the standard protected constructor for an exception. Is there an appropriate way to throw this exception from the mock object / Expect.Call statement?

like image 708
BlackWasp Avatar asked Mar 02 '23 05:03

BlackWasp


2 Answers

Reflection can break the accessibility rulez. You will void the warranty, a .NET update can easily break your code. Try this:

using System.Reflection;
using System.Messaging;
...
        Type t = typeof(MessageQueueException);
        ConstructorInfo ci = t.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, 
          null, new Type[] { typeof(int) }, null);
        MessageQueueException ex = (MessageQueueException)ci.Invoke(new object[] { 911 });
        throw ex;
like image 88
Hans Passant Avatar answered Mar 12 '23 20:03

Hans Passant


You can cause this by trying to create an invalid queue. Probably safer then being held captive by framework changes (through using private/protected constructors):

MessageQueue mq = MessageQueue.Create("\\invalid");
like image 37
TheSoftwareJedi Avatar answered Mar 12 '23 20:03

TheSoftwareJedi