This post is the second part of the post NAS Tutorial which is based on KB article 861762, previously published on PartnerSource. This post is an updated version, and available to a wider audience (no PartnerSource login required).
This post describes how to use NAV Communication Component (Comcom) and NAV Bus adapter. Comcom is designed to enhance communication between Navision and other applications, like a Web server, a BizTalk server or some other external system. What kind of communication takes place, is all down to the specific application (like Commerce Portal or Commerce Gateway). In this document, we will only look at the mechanisms of sending and receiving a document.
This post also shows how to create a very small application that can send a message to MS Message queue, and receive this message again.
=== Updated on July 15th ===
I was made aware of a couple of important additional things to remember when working with MSMQ Busadapter:
1) Messages sent to NAS must have the label "Navision MSMQ-BA"
2) Text in the messages must be in the format UTF-8. You cannot use UNICODE.
There are more details about this here.
=== End of Update ========
What is Comcom and Bus adapter?Both Comcom and the bus adapter are automation servers developed by Microsoft for NAV. Comcom is a general communication component. All it does, is sending and receiving messages. You need to use a bus adapter with Comcom. The bus adapter is specific to the media used to transport the messages that Comcom sends and receive. At the moment there are two bus adapters: One for MS Message queue, and one for Named Pipes. In this document we will use the bus adapter for MS Message queue (MSMQ).
The reason for having Comcom as well as a bus adapter is, that you can create your application using Comcom without being concerned about what the transport media is going to be. When you decide on that / or if you want to change the media used, it will only require small changes – the application itself would need only very minor changes.
PrerequisitesNAV communication components come as part of SDK (Devkit) on the product CD. This needs to be installed before you start. And, you need to have MSMQ installed (installs as part of Windows).
First, create a new queue in MSMQ: Open Computer Management, expand "Services and Applications", and you should see "Message Queuing" here, if MSMQ is installed. Expand "Message Queuing", and then expand "Private Queues". Note that the MSMQ Busadapter is only tested with private queues, and is unlikely to work with public queues.
Rightclick on "Private Queues", select New -> Private Queue, and enter a name. Let's call it MuQueue. Also note that MSMQ Busadapter has not been tested for Transactional queues, so leave the field "Transactional" blank.
Now you have the queue, and the rest is up to NAV to send a message into this queue.
So, in NAV, create a new codeunit. First we need do declare the following variables to get access to Comcom:Name Type SubtypeComcom Automation 'Navision Communication Component version 2'.CommunicationComponentComOut Automation 'Navision Communication Component version 2'.OutMessageOutStr Outstream All automation variables need to be initialised before they can be used. Type these two lines to initialise Comcom and ComOut:
CREATE(Comcom);ComOut := Comcom.CreateoutMessage('Message queue://');
'Message queue://' is the actual word Message queue. We need to tell Comcom to send the message in a format for Microsoft Message queue. But this is the only reference to the media (until we get to the bus adapter).
Remember, Comcom has only one purpose: Sending / receiving messages. It is for writing the actual message that we want to send that we need OutStr. OutStr will contain the message, and Comcom will send it.
To associate Comcom with OutStr, add this line:
OutStr := ComOut.GetStream;
Then, compose the message using OutStr:OutStr.WRITE('This is my message.');
Once the message is done, we just need to send it. Send it in this way:ComOut.Send(0);
That’s the application done. Finally, we just need to add our bus adapter, which needs to be specific to what transport media we are using - in this case MSMQ.Add one more global variable:Name Type SubtypeMQBus Automation 'Navision MS-Message Queue Bus Adapter'.MSMQBusAdapter
To “plug in” the bus adapter, type in these lines at the beginning of the codeunit:
create(MQBus);MQBus.OpenWriteQueue('My-pc2\Myqueue',0,0);// On Win2000, use MQBus.OpenWriteQueue('.\private$\Myqueue',0,0);Comcom.AddBusAdapter(MQBus,0);
Your final codeunit should now look like this:
ComOut := Comcom.CreateoutMessage('Message queue://');OutStr := ComOut.GetStream;OutStr.WRITE('This is my message.');
To test is, try to run it, then look the queue and see if the message was sent.
Next, we want to receive this message.Receiving a message
Create a new codeunit with these variables:
Name Datatype SubTypeComcom Automation Navision Communication Component version 2'.CommunicationComponentInMsg Automation 'Navision Communication Component version 2'.InMessageInStr InStream MSMQBus Automation 'Navision MS-Message Queue Bus Adapter'.MSMQBusAdapterLine Text 250
Highlight the Comcom variable, go to properties, and set the property WithEvents to Yes. This will create a new trigger called:Comcom::MessageReceived(VAR InMessage : Automation "''.IDISPATCH")
This trigger will automatically run whenever there is a new message in the queue.
The finished codeunit should look like this:
Comcom::MessageReceived(VAR InMessage : Automation "''.IDISPATCH")InMsg := InMessage;InStr := InMsg.GetStream;InStr.READTEXT(Line);MESSAGE(Line);
Go to Codeunit Properties, and set the property SingleInstance to Yes. Now, you can either run it from the object designer, or you set up NAS to run this codeunit.
So you should now have 1 codeunit to send a message to MSMQ, and another codeunit which can listen to this queue, and which is triggered as soon as a message arrives.
Microsoft Dynamics UK
Microsoft Customer Service and Support (CSS) EMEA
These postings are provided "AS IS" with no warranties and confer no rights. You assume all risk for your use.