Forum Discussion

slotz's avatar
slotz
New Contributor
13 years ago

Using the System.Messaging.MessageQueue Object in C#Script

Hello,

Below is an example of C# that will read a private MSMQ queue

System.Messaging.MessageQueue queue = new
System.Messaging.MessageQueue(".\\Private$\\MyPrivateQueue");
queue.Send("Hello world");
System.Messaging.Message msg = queue.Receive();
msg.Formatter = new System.Messaging.XmlMessageFormatter(
new string[] {"System.String"});
Console.WriteLine(msg.Body);

How do you code this in C#Script?



Thanks

3 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Shawn,



    I'd recommend using MSMQ scripting API instead. You can find a sample script that demonstrates how to send and receive MSMQ messages here. It's a VBScript script, but it should help you get the idea.



    But if you want to use .NET classes for this purpose, you can access them via the dotNET object. To make MSMQ .NET classes available for scripting, you'll need to add the System.Messaging.dll assembly to Tools > Current Project Properties > CLR Bridge. After that, you can write code like this:

    (".\\Private$\\MyPrivateQueue");

    queue["Send"]("Hello, world");

    // and so on
  • slotz's avatar
    slotz
    New Contributor
    Thank you for the reply Helen.



    My preference is to use the MSMQ scripting API  but I was having no luck with the c#script code.  Here is my current attempt.



    var msmqInfo;

    var qname = ".\\private$\\TestQueue"

    msmqInfo = Sys["Messaging.MessageQueue"];

    //set path for messaging object

    msmqInfo["Path"](qname);



    Testcomplete complains that the variable object 'msmqInfo' is undefined.  Any help would be appreciated.
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Shawn,



    TestComplete's C#Script uses bracket notation to access object properties and methods:

     = new_value;

    variable = object["property"];



    object["method"]();

    object["method"](parameters);


    So instead of

    msmqInfo = Sys["Messaging.MessageQueue"];

    msmqInfo["Path"](qname);
    it should be

    ("Messaging.MessageQueue");

    msmqInfo["Path"] = qname;




    It seems to me that you are somewhat confused by this bracket syntax. I'd like to note that, despite the name, C#Script has very little to do with C#. It's a special-purpose language primarily designed to create Connected applications. So, unless you are going to do Connected apps, I'd recommend not using C#Script because it can be hard to read.



    For a C#-like language with the dot syntax, use JScript.

    In JScript, the same code is as follows:

    msmqInfo = Sys.OleObject("Messaging.MessageQueue");

    msmqInfo.Path = qname;