sFTP with Dynamics AX 2012

 In Development, Dynamics AX

sFTP

Due to the sensitivity of data handled by an ERP, being able to transfer files using sFTP in Dynamics AX is a good method to ensure the confidentiality of data, both on the internet, and on the company’s own intranet.

Until the date of publication of this entry Microsoft has not published any library for AX, nor for .NET, that implements sFTP.

putty

One solution may be to install a program like putty and run it from AX. The main drawback is the discomfort of having to install the program on all AX clients and writing an API in X++ to format the putty commands that are to be executed in the operating system.

SSH.NET

Much better solution is to use .NET libraries like ssh.net. The .dll assembly deployment system since the 2012 version of Dynamics AX and access to the API structure with intellisense in the X++ editor makes file transfer solutions simpler, self-contained and elegant.

is easy to find tutorials and examples of how to use ssh.net to transfer files with sFTP with C#, information that can be used almost as it is in X++.
But as the devil hides in the details, we will take a look at a stumbling block and how to avoid it, so that sFTP, files transfer, even if not a walk in the park, is at least a walkable path.

THE PROBLEM

Method access in X++ to a C# library is not exactly the same as in C#itself, and ssh.net library has an optional argument in the methods DownloadFile and UploadFile of type Action<ulong> inside the SftpClient class. These parameters have the utility of providing the functionality of calling a method that is executed in a new thread for any extra functionality required when uploading or downloading a file.
This parameter is mandatory in X++ and thus needs an Action<ulong> value in the method call.
The problem is that the Action type has no equivalent in X++, and therefore can not generate a valid null value (which turns out to be the default value in the two methods we are dealing with). We’ll look closer at the DownloadFile method (UploadFile method is analogous).

The method call from X++:

sftpClient.DownloadFile(_file, fs, null);

Returns the error:

DownloadFile is not a member of SftpClient

Since there is no DownloadFile method in the class that matches the “signature” of the call

The solution

Fortunately, the ssh.net library has a MIT license and the source code is public, so you only have to overload the two methods that occupy us by removing the optional parameter and call the existing method from the new one. This way, the previous functionality in the library is not lost and we can send and receive files using sFTP without problems with AX.

The existing method is as follows:

public void DownloadFile(string path, Stream output, Action downloadCallback = null)
{
  CheckDisposed();

  InternalDownloadFile(path, output, null, downloadCallback);
}

Just add a new method like the following:

public void DownloadFile(string path, Stream output)
{
  DownloadFile(path, output, null, null);
}
Recommended Posts

Leave a Comment