Asynchronous web services in NAV

Asynchronous methods are very useful for web services.  To be able to start a process with one method and then check the status with another opens a lot of possibilities.

There are two known patterns in NAV that support asynchronous methods;  one is to use the STARTSESSION function to execute the business logic in another thread, the other is to create a job queue entry and let NAS handle the task.

I was talking to a client this morning and suggested using the asynchronous web services for his tasks.  I decided to write some code and test the functionality to see if my theory was working.

The first thing I need is a table for the requests.  This table contains the identification for the queue, the status and other details.

ProcessQueueTable

A single web service method is used to create a new queue entry.

AsyncWebService

I like to use the new TryFunction for my web services.  Note that when the queue has been inserted I fire an event with the newly created queue entry.  This means that I can extend this web service with events without having to modify the web service signature.

Each process needs a dedicated Codeunit.  That Codeunit is built on top of the queue record and thereby using the parameter table pattern.

TheAsyncJob2

Note that this Codeunit is also using the TryFunction and taking care of the rollback if that function fails.

The process Codeunit catches the integration event and checks the process code before starting then selected task.

ProcessStarter2

So, if I ask the web service to start a job called StartAsyncAdjustCostItemEntriesProcess, NAV will start a new session to execute the adjust cost for item entries.  The process that creates the queue and starts a new session is very quick so the response from the web service is almost instant.

Then we wait for a moment and ask the web service about the status for the newly created queue.

CheckQueueStatus

Again using the TryFunction to make sure that my web service will have a proper response to the query.

It is quite easy to extend this module by catching the OnNewQueueInserted event for each process code you build support for.

A sample C# code that I used to test this looks like this

CSharpDemoCode2

As you can imagine we can put anything into the ProcessData variable.  By using Base64 encoding anything can be converted to a text variable.

I hope this will turn on some lights and you will be able to use this in your daily work.

Attached is a zip file with the NAV objects and the C# project.

NAVAsyncWebService

 

 

 

 

6 Replies to “Asynchronous web services in NAV”

  1. Very interesting! Thank you. I was wondering if I could remotely invoke a NAV job that could take some time to finish… Assuming I am not interested whether it was successful or not. From another NAV server, of course. In the above example you are using external references which lets you somehow “keep” the referenced object to monitor for changes. Do you think that the same could be achieved from C/AL + .NET?

  2. Could you please explain what is the ASSERTERROR ERROR(”) instruction doing? You have a GETLASTERRORTEXT after that, which should return you ” …You are basically blanking the error text? Am I missing something?

  3. Hi Gunnar
    I need you help to be able to integrate a some portal tables into NAV table.
    Though i have knowledge in NAV coding but I don’t have technical knowledge about integration with other external table. Though I came across using REGISTER CONNECTION which I try and world but am not sure if this will be effective in a production environment. I want to learn as a novice and I will appreciate your support. Many Thanks

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.