Microsoft Dynamics 365 Blog

 Last week I had the need to use the MSMQ adapter in AX2009. Setting up the AX and Windows side of this was pretty straightforward as there is a great article on TechNet detailing how to do this ( This meant I could generate outgoing messages from AX pretty easily. However when I wanted to test incoming messages I needed to write some code to create messages in MSMQ which could be successfully consumed by AX’s inbound AIF channel.

The X++ job below will create a message in an MSMQ queue which can be read back in by AX’s inbound AIF MSMQ process. There are a few points to note about this:

– The queue created in MSMQ is transactional and authenticated is checked. The account of the user running this X++ job has to be granted permissions on the queue to be allowed to write to it. The account of the AOS has to be granted permissions to be allowed to read from the queue (for the inbound AIF process to run).
– The code below uses Microsoft.Dynamics.IntegrationFramework.Adapter.dll just because I am familiar with it, it is not a requirement to use this, you can generate the XML however you want.
– We use System.Messaging.MessageQueueTransaction because the queue is transactional, without this no message would be generated to the queue and also no error would be raised.
– We use the streamwriter to preserve the format of the XML, setting the message.body() direct will result in incorrectly encoded XML in the message body.
– We set AttachSenderId and UseAuthentication – these are required for the message to be readable by AX.
– This is written in X++, but as it uses managed code to do all the work it can be simply transposed to C# or whichever language you like.

To use this just set up a new public queue on your machine called “MyTest”, check transactional and authenticated and grant full control to your AOS account and your own account. Add the attached XML file in C:\Aif\. Then run the job, it will create a message! Now you can configure and inbound AIF channel in AX which looks at your myTest queue, enable it for the salesOrder.create action and AX will be able to read from it MSMQ queue and create a sales order.

static void MSMQ_outbound(Args _args)
    System.Messaging.MessageQueue               messageQueue;
    System.Messaging.Message                    message;
    System.Messaging.MessageQueueTransaction    transaction;
    Microsoft.Dynamics.IntegrationFramework.Adapter.FileSystem fileSystem;
    Microsoft.Dynamics.IntegrationFramework.Adapter.TransportMessage transportMessage;
    System.IO.StreamWriter  streamWriter;
    fileSystem = new Microsoft.Dynamics.IntegrationFramework.Adapter.FileSystem();
    transportMessage = fileSystem.ReadFile(‘C:\\AIF\\SalesOrderCreate.xml’);

            transaction = new System.Messaging.MessageQueueTransaction();
            messageQueue = new System.Messaging.MessageQueue(@’.\MyTest’);
            message = new System.Messaging.Message();

            streamWriter = new System.IO.StreamWriter(message.get_BodyStream());
            messageQueue.Send(message, transaction);

        info(“An error occurred”);

    transportMessage = null;
    fileSystem = null;

    info(“Message sent”);



We're always looking for feedback and would like to hear from you. Please head to the Dynamics 365 Community to start a discussion, ask questions, and tell us what you think!