Create an Azure function

Completed

Using .NET interop in the Business Central SaaS version isn't possible. By using Azure Functions, you have a possible solution if you still need to connect to .NET libraries. You can directly develop in C# by using the Azure portal code editor, but you can also develop your own .NET DLL libraries and use them within your Azure Function app.

The following code example shows a small .NET Class Library being created. In the Class Library, you can find one class, BlackAndWhiteConverter, which will apply grayscale on an image. The image will be passed as a parameter by using HttpRequestMessage. Therefore, an external library ImageProcessor is used.

The following sample also shows the C# implementation.

using ImageProcessor;
using ImageProcessor.Imaging.Filters.Photo;
using System.IO;
using System.Net.Http;

namespace BlackAndWhitePicture
{
    public class BlackAndWhiteConverter
    {
        public HttpResponseMessage Convert(HttpRequestMessage request)
        {
            var stream = request.Content.ReadAsStreamAsync().Result;
            byte[] outArray;
            using (var outStream = new MemoryStream())
            {
                using (var imageFactory = new ImageFactory())
                {
                    imageFactory.Load(stream)
                        .Filter(MatrixFilters.GreyScale)
                        .Save(outStream);
                }
                outArray = outStream.ToArray();
            }

            var result = new HttpResponseMessage(HttpStatusCode.OK);
            result.Content = new ByteArrayContent(outArray);
            result.Content.Headers.ContentType = 
                   new MediaTypeHeaderValue("image/jpg");
            return result;
        }
    }
}

The result of a .NET Class Library is a .DLL file that you can upload to Azure and use in an Azure function. To upload a .NET library, you need to open (https://<yourfunctionapp>.scm.azurewebsites.net), and then put .scm before the azurewebsites.net domain name. After you have signed in, you can open a command prompt under the Debug Console menu, where you can browse your app folder and create a subfolder called bin. You can drag and drop your .NET DLL files to this bin folder.

Screenshot of the Kudu app environment page.

When the .NET files are uploaded, you can use them in your Azure function. Therefore, you need to use the special #r syntax. This syntax is used to reference external assemblies. In the following example, the Azure function uses the .NET Library from the previous example.

#r "D:\home\site\wwwroot\ImageConverter\bin\BlackAndWhitePicture.dll"
#r "D:\home\site\wwwroot\ImageConverter\bin\ImageProcessor.dll"

using System.Net;
using System.Net.Http.Headers;
using System.Drawing;
using BlackAndWhitePicture;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    var converter = new BlackAndWhiteConverter();
    return converter.Convert(req);
}