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.
A single web service method is used to create a new queue entry.
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.
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.
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.
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
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.