NAS Tutorial

This post is based on KB article 861762 which was previously published on PartnerSource. This post is an updated version, and available to a wider audience (no PartnerSource login required).

It describes the basics of what Navision Application Server (NAS) does, and an example of how to build a NAS application in less than 30 minutes. The KB article continued with an example of how to use NAV Communication Components and BusAdapter.

What is NAS?
NAS is a client without a user interface (GUI), running as a service. That’s all it is. When it starts, it will run a function in codeunit 1, passing on the Start-up parameter that has been set up. That’s all it does. There are two prerequisites for this function in codeunit 1:

It must have ID 99 (the name of the function is irrelevant)
It must have one parameter Text 250. The name of this parameter is also irrelevant.

NAS on its own does not do anything else. So on its own it is a very simple component. But the application you build around it can, off course, be as complicated as any other application.

This tutorial will take you through the steps of building a simple application using NAS. After having completed the tutorial, I hope you will be comfortable with NAS, how to use it, and how to set it up.

Tutorial:

1:  Setting NAS

In this tutorial we will run NAS on a SQL Server. Just like with a normal client you can do the same things against a Native server. Just use Nas.exe instead of NasSQL.exe. I assume that you already have a NAV database running on a SQL Server, and that you can open this database with a normal NAV client.

Install NAS from the product CD.

Installing NAS will automatically create two services [Machinename-CLASSIC] and [Machinename-SQL] (display name is “Navision Application Server [Machinename]-CLASSIC/SQL”).
Go to services and check that you can see these two services. These services are ready to use but just for the practise, the first thing we will do is to remove these two services and create our own service.

There are three interfaces you can use to administer NAS:
Navision Application Server Manager (an MMC snapin)
Windows Registry
Command prompt

There are only two tasks that you can only achieve from the command prompt: Removing an instance (service) of NAS and adding an instance of NAS. All other tasks for administering NAS can be achieved using either of these three tools.

Remove the two services and add your own service:
For this task, you need to use the command prompt. Go to the folder where you installed NAS. Notice that, in this folder you will find two .exe files: Nas.exe and Nassql.exe. In the following section, use nassql.exe since we are running on a SQL Server. If you were running NAV on Native server, you would use nas.exe. But each file has the same functionality.

Also note, that if you are running on Windows Vista, then opening a normal command prompt may not give you enough permissions. So, if running on Vista, open the command prompt from the Start Menu -> All Programs -> Accessories, then right-click on “Command Prompt”, and select “Run as Administrator”.

To remove a service, from the command prompt in the NAS folder, type:
Nassql appservername=[machinename]-CLASSIC, uninstallasservice .
This will remove the service with the name [machinename]-CLASSIC that was installed when you installed NAS.

In the same way, remove the [Machinename]-SQL service.

To add a service, type:
Nassql appservername=NAS, installasservice

This will add a service called NAS. This is the service that we will use in this tutorial. You can add as many instances of NAS as you like – each one will appear as a service. If you want to run NAS from a different folder, then just copy the NAS files into that folder, and run the command prompt commands from there.

Set up the NAS service;
Once you have an instance (service) of NAS, you will need to set it up. You can do this from the command prompt, but it is easier to user either the NAS manager, or Windows Registry. If you want to use Registry (Regedit), go to the key HKEY_LOCAL_MACHINE/SYSTEM/CurrentConotrolSet/Services and look for your service here. We will use the NAS Manager.

Remember from the previous step, that the name of your NAS is “NAS”. But in Services, the display name will be “Application Server for Microsoft Dynamics NAS” if you are using version 5. Or “Navision Application Server NAS” if you are using version 4. In the next steps, use the name NAS.

To start NAS Manager, click Start, Programs, Microsoft Dynamics NAV, Navision Application Server Manager. Rightclick on “Application Server for Microsoft Dynamics NAV”, and select New -> Application Server. Then type in the name of your NAS service (“NAS”).

The parameters you need to set up are:
Database Server Name: This is your SQL Server.
Database:  The NAV database.
Company Name: Specify the name of the Navision company. NAS must be set up to run in a specific company. If you have more than one company, you need one NAS for each company.
Start-up parameter:  Specify any text string here. You can use this parameter to distinguish between multiple instances of NAS. For now, just specify “NAS”.
Net type:  Select Default.
Object Cache: Specify Object Cache, or leave it as default.

Once this is done, click Apply these settings.

One more thing you have to do, is to associate your NAS service with a Windows account. Do this by going into services, right-click the NAS service and select Properties. On the “Log On” tab, under “Log on as:”, click This Account, and select an account. For now, you can just select the Administrator Account. In a real installation it is recommended that you create a new Windows Account and use that just for NAS. NAS can only use Windows Authentication. It cannot use database login.

NAS has now been set up.

2:  Changes needed in NAV

In NAV, create a Windows Login
In the previous section you set up NAS to use a Windows Account. You need to give access in NAV for this account. Go to Tools, Security, Windows Logins and specify that account here. For this exercise, click Roles and give this account role ID SUPER. Make sure to synchronize permissions (Tools -> Secutiry -> Synchronize all logins).

Modify codeunit 1
The last step is to create a function in codeunit 1. As mentioned before, NAS runs a function in codeunit 1 with the ID 99 and a parameter (text 250). In a standard NAV database, this function is called NASHandler. The code in this function depends on which version of NAV you are running, but in recent versions, it will contain a section that looks like this:

IF CGNASStartedinLoop = FALSE THEN
CASE Parameter OF
‘OSYNCH’,’JOBQUEUE’:
BEGIN
IF NOT JobQueueSetup.GET THEN BEGIN
JobQueueSetup.INIT;
JobQueueSetup.INSERT;
END;

This part of the code tells NAS to do, if it logs on with Startup Parameter OSYNCH or JOBQUEUE. So, add your own parameter here, like this:

IF CGNASStartedinLoop = FALSE THEN
CASE Parameter OF
‘NAS’:
BEGIN
// This is your new section, and where you will tell your NAS what to do.
END;
‘OSYNCH’,’JOBQUEUE’:
BEGIN
IF NOT JobQueueSetup.GET THEN BEGIN
JobQueueSetup.INIT;
JobQueueSetup.INSERT;
END;

You now have your NAS application, but at the moment it’s not doing anything. Let’s make the simplest application in the world: Put one line in this new section:
MESSAGE(‘NAS Has logged in!’);
Close (and safe) the codeunit.

Test it!
Now it’s time to see if everything has been set up correctly. Because NAS doesn’t have a user interface, it cannot open a message box. Instead, it will put any messages from the MESSAGE command as a message in the application log (Event Viewer). Check that NAS is working by following these steps:

1) Go to Services, and (re)start the NAS Service. Just because NAS starts, it doesn’t necessarily mean that it works!
2) Open the Even Viewer (Start, Programs, Administrative tools, Even Viewer. Click “Application Log” and check the latest messages here. If you see a message saying ‘NAS Has logged in!’ it means that NAS has successfully run the function in codeunit 1.

Make an Application using the timer trigger:

Create a new codeunit. Declare one variable:

Name Datatype Suntype
NavisionTimer Automation ‘Navision Timer 1.0’.Timer

Set the property WithEvents to Yes on this variable. Notice the new trigger that was created.

Add this code:
OnRun()
IF ISCLEAR(NavisionTimer) THEN
CREATE(NavisionTimer);

NavisionTimer.Interval := 3000;
NavisionTimer.Enabled := TRUE;

NavisionTimer::Timer(MilliSecounds : Integer)
MESSAGE(‘Time is %1’,TIME);

Run  the codeunit and see that Nothing happens.  – why is that?

Design the codeunit again, go to codeunit properties and set the property Singleinstance to Yes.
Run the codeunit again and see the difference that this property made. SingleInstance means that the codeunit will keep running, waiting to be triggered by something – in this case, the timer.

The only way to stop a SingleInstance codeunit, is to close or reopen the company (Ctrl+O).

Now go back to codeunit 1, and replace the code in function 99 with this line:

CODEUNIT.RUN(CODEUNIT::”Navision Timer”);

Because of Object Cache, NAS may not pick this change up until you restart the service. So do that, and if everything works OK, you should now get the time in your application log every three seconds.

Troubleshooting NAS:

Remember that NAS is just a client without a user interface. So the simplest way to troubleshoot NAS, is to run whicever codeunit it runs from a normal NAV client. You can then better see what it is doing, and you can use the normal debugging features.