Saturday, October 9, 2010

NAV Communication Components

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.
Prerequisites
Microsoft Dynamics NAV communication components come as part of SDK on the product CD. This needs to be installed before you start. And, you need to have MSMQ installed.
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.
Right click on "Private Queues", select New -> Private Queue, and enter a name. Let's call it MQueue. 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 Microsoft Dynamics NAV to send a message into this queue.
Create a new codeunit. First we need do declare the following variables to get access to Comcom:
Name Type Subtype
Comcom Automation 'Navision Communication Component version 2'.CommunicationComponent
ComOut Automation 'Navision Communication Component version 2'.OutMessage
OutStr Outstream

All automation variables need to be initialized before they can be used. Type these two lines to initialize 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;
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 Subtype
MSBus 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(MSBus);
MSBus.OpenWriteQueue('\Myqueue',0,0);
Comcom.AddBusAdapter(MSBus,0);
Your final codeunit should now look like this:

OnRun()

CREATE(Comcom);
CREATE(MSBus);
MSBus.OpenWriteQueue('Computername\Myqueue',0,0);
Comcom.AddBusAdapter(MSBus,0);
ComOut := Comcom.CreateoutMessage('Message queue://');
OutStr := ComOut.GetStream;
OutStr.WRITE('This is my message.');
ComOut.Send(0);

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 SubType
Comcom Automation Navision Communication Component version 2'.CommunicationComponent
InMsg Automation Navision Communication Component version 2'.InMessage
InStr InStream
MSREBus Automation 'Navision MS-Message Queue Bus Adapter'.MSMQBusAdapter
Line 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:

OnRun()
CREATE(Comcom);
CREATE(MSReBus);
MSREBus.OpenReceiveQueue('computername\private$\lll',1,1);
Comcom.AddBusAdapter(MSREBus,1);
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.
First 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.

Saturday, May 15, 2010

Two instances of Microsoft Dynamics NAV Server on a single server computer

Prerequisites•Microsoft SQL Server 2008 or SQL Server 2005 on a computer running Windows Server 2008 or Windows Server 2003.
• Microsoft Dynamics NAV 2009 Database Components and the first Microsoft Dynamics NAV Server on the same server computer.
• RoleTailored client on a separate computer to test the installation.

To create a second service
In Control Panel, click Administrative Tools, and then click Services to open the Services snap-in.
In the list of services, right-click Microsoft Dynamics NAV Server, and then click Stop.
Navigate to the Microsoft Dynamics NAV Service directory. This directory is typically located at C:\Program Files\Microsoft Dynamics NAV\60\.
Copy the service directory and paste it into the same directory. Rename the copy from Copy of Service to Service2.
At the command prompt, type the sc command to create a new service. Copy the following command and paste it at the command prompt. Replace with the actual computer name.
sc \\ create MicrosoftDynamicsNAVServer$NAV2 binpath= "C:\Program Files\Microsoft Dynamics NAV\60\Service2\Microsoft.Dynamics.Nav.Server.exe $NAV2" DisplayName= "Microsoft Dynamics NAV Server Instance 2" start= auto type= own depend= NetTcpPortSharing
Note
You can also specify a login account for the service as part of the sc command using the obj parameter, such as:
obj= "NT Authority\NetworkService"
If the command succeeds, then you receive the following message:
[SC] CreateService SUCCESS

In the Services snap-in, you see your second service, Microsoft Dynamics NAV Server Instance 2, in the list.
Double-click the new service to open a Properties dialog box for the new service, and then click the Log On tab.
The default setting is for the service to log on with the Local System Account. Setup configures the original Microsoft Dynamics NAV Server service to use the Network Service account. also uses the Network Service account for the second service. For improved security, you should use a dedicated domain user account. For information on how to configure a Microsoft Dynamics NAV Server service to use a domain user account, see Walkthrough: Installing the Three Tiers on Three Computers.
Click This account on the Log On tab.
Type NT Authority\NetworkService in the first text box.
Clear the entries in the Password and Confirm password fields.
Click OK to exit the Properties dialog box for the new service.
Right-click Microsoft Dynamics NAV Server Instance 2, and then click Start to verify that the service is configured correctly.
Right-click Microsoft Dynamics NAV Server Instance 2 again, and then click Stop.
Configuring a Second Service

Before Simon can start to use the second service, he must create a name for the new server instance. He must also activate port sharing so that multiple clients can share the same port number.
To create a name for the server instanceUse Notepad or another text editor and open the CustomSettings.config file in the Service2 directory. The default path for this file is C:\Program Files\Microsoft Dynamics NAV\60\Service2\Custom.Settings.config.
Locate the following line:

Replace DynamicsNAV with DynamicsNAV2, which is the name for your new server instance.
Save your changes, and then close the editor.
To activate port sharing
Return to the Services snap-in.
Right-click Net.Tcp Port Sharing Service, and then click Properties.
On the General tab, set Startup type to Manual.
Click OK to close the Properties window.
Right-click Net.Tcp Port Sharing, and then click Start.
You have now activated port sharing over the TCP/IP protocol.

Note
You can also specify a separate port number for each Microsoft Dynamics NAV Server service. You then do not need to implement TCP port sharing.
It is not possible to run the Microsoft Dynamics NAV Server service with port sharing if the service is running on a domain user account. If you need to run two Microsoft Dynamics NAV Server services on the same computer, then you must run the services with the Network Service account.
Recreate the Original Microsoft Dynamics NAV Server Service with TCP Port Sharing

To reconfigure the original Microsoft Dynamics NAV Server service to use TCP port sharing, you must use the sc command to delete the service and then use it again to recreate the service with the proper configuration.
To recreate the original Microsoft Dynamics NAV Server service
At the command prompt, type the following command to delete the Microsoft Dynamics NAV Server service:

sc delete MicrosoftDynamicsNavServer

Note
If you attempt to delete a service while it is running, then the service is marked for deletion but is not deleted until you restart the computer.
At the command prompt, recreate the original Microsoft Dynamics NAV Server service with the option for TCP port sharing:
sc \\ create MicrosoftDynamicsNAVServer binpath= "C:\Program Files\Microsoft Dynamics NAV\60\Service\Microsoft.Dynamics.Nav.Server.exe" DisplayName= "Microsoft Dynamics NAV Server" start= auto type= own depend= NetTcpPortSharing.

If the service has been created successfully, then you receive the following message:

[SC] CreateService SUCCESS

If the Services snap-in is still open, then close it and reopen it to update the information in the snap-in. Otherwise, reopen the Services snap-in.
Double-click Microsoft Dynamics NAV Server to open the Properties dialog box for the service, and then click the Log On tab.
Click This account on the Log On tab.
Type NT Authority\NetworkService in the first text box.
Clear the entries in both the Password and Confirm password fields.
Click OK to exit the Properties dialog box for the new service.
In the Services snap-in, right-click Microsoft Dynamics NAV Server, and then click Start.
Right-click Microsoft Dynamics NAV Server Instance 2, and then click Start.
You now have two instances of the Microsoft Dynamics NAV Server service running on the same computer.

Testing the Second Service

To test second service by connecting to it from the RoleTailored client and by making a change to the data in one database.
To connect the RoleTailored client to a second service.
Open the RoleTailored client that you have installed on a separate computer.
In the Role Center menu bar, click Microsoft Dynamics NAV, and then click Select Server.
In Server Name, add the number 2 to the DynamicsNAV value so that it is DynamicsNAV2.
Click Connect.
Select CRONUS International Ltd. from the Companies list.
Unless you have already installed additional databases, the demo database is the only one available and is automatically selected.
Click Connect, and then start working with your database.
Creating a Second Web Services Server on a Single Computer

You can adapt the procedure that is described, to create a second Microsoft Dynamics NAV Web services server on a single computer. Whether a service that you create manually is a standard Microsoft Dynamics NAV Server service or a Microsoft Dynamics NAV Web services service is determined by the name you give the service. If the name starts with "MicrosoftDynamicsNavWS", then the resulting service will be a Web service. If the name starts with anything else, then it will be a standard Microsoft Dynamics NAV Server service, which is a service serving RoleTailored clients.