How to Run Aspose.CAD Docker image in Azure Function

Prerequisites

  • Docker must be installed on your system. For information on how to install Docker on Windows or Mac, refer to the links in the “See Also” section.
  • Visual Studio 2022.
  • NET 6 SDK is used in the example.
  • Postman

Azure Function

In this example, you create a simple conversion function that converts a CAD file and saves it as an image. The application can then be built in Docker and run in Azure Function.

Creating the Azure Function

To create the Azure Function program, follow the steps below:

  1. Once Docker is installed, make sure that it uses Linux Containers (default). If necessary, select the Switch to Linux containers option from the Docker Desktops menu.
  2. In Visual Studio, create a NET 6 Azure Function.
    A NET 6 Azure Function project dialog
  3. Additional information.
    A NET 6 Azure Function project dialog
  4. Install the latest Aspose.CAD version from NuGet.
    Aspose.CAD on NuGet
  5. Since the application will be run on Linux, you may be needed to install additional fonts. You could prefer ttf-mscorefonts-installer.
  6. When all required dependencies are added, write a simple program that creates an ellipse and saves it as an image:
public static class Function1
{
    [FunctionName("convert")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");
        try
        {
            using (var image = (CadImage)Image.Load(req.Body))
            {
                var ms = new MemoryStream();
                image.Save(ms, new PngOptions());

                ms.Seek(0, (System.IO.SeekOrigin)SeekOrigin.Begin);

                return new FileContentResult(ms.ToArray(), "application/octet-stream")
                {
                    FileDownloadName = "Export.png"
                };
            }
        }
        catch (Exception e)
        {
            return new OkObjectResult(e.Message);
        }
    }
}

Configuring a Dockerfile

The next step is to create and configure the Dockerfile in root project folder.

  1. Create the Dockerfile and place it next to the solution file of your application. Keep this file name without extension (the default). Root project folder
  2. In the Dockerfile, specify:
FROM mcr.microsoft.com/azure-functions/dotnet:4 AS base
WORKDIR /home/site/wwwroot
RUN apt-get update
RUN apt-get install -y apt-utils
RUN apt-get install -y libgdiplus
RUN apt-get install -y libc6-dev 
RUN ln -s /usr/lib/libgdiplus.so/usr/lib/gdiplus.dll
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Aspose.CAD.Function/Aspose.CAD.Function.csproj", "Aspose.CAD.Function/"]
RUN dotnet restore "Aspose.CAD.Function/Aspose.CAD.Function.csproj"
COPY . .
WORKDIR "/src/Aspose.CAD.Function"
RUN dotnet build "Aspose.CAD.Function.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Aspose.CAD.Function.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /home/site/wwwroot
COPY --from=publish /app/publish .
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

The above is a simple Dockerfile, which contains the following instructions:

  • The SDK image to be used. Here it is the Net 6 image. Docker will download it when the build is run. The version of SDK is specified as a tag.
  • After, you may need to install Fonts because the SDK image contains very few fonts. Also, you can use local fonts copied to docker image.
  • The working directory, which is specified in the next line.
  • The command to copy everything to container, publish the application, and specify the entry point.

Docker Hub

  1. Login Docker Hub
  2. Create public Repository

Building and Running the Application in Docker

Now the application can be built and run in Docker. Open your favorite command prompt, change directory to the folder with the application (folder where the solution file and the Dockerfile are placed) and run the following command:

//example
docker build -t <user name>/<repository name> .

docker build -t user/asposefunction .

The first time you run this command, it may take longer because Docker needs to download the necessary images. After the previous command completes, run the following command to push the image to docker hub:

//example
docker push <user name>/<repository name>:tagname

docker push user/asposefunction:latest

Azure

  1. Login Azure.
  2. Choose Azure services.
  3. Choose Function App and create a function.
    Azure create function button
  4. Repeat the basic settings as in the image below.
    Azure create function settings
  5. Click ‘Review + create’ -> Create.
  6. Wait for deployment to finish.
  7. Click ‘Go to resorce’ button.
    Resource button
  8. Stop aspose-cad-docker-example function.
    Stop conteiner
  9. Go to the deployment center menu and make the appropriate settings.
    Deployment center
  10. Save settings
  11. Copy Webhook URL from deployment center settings.
    Webhook url
  12. Go to Docker Hub, select your repository and select webhooks.
  13. Paste the ‘Webhook url’ from Azure into the Docker Hub webhook url and set the name.
    Webhook settings in docker
  14. Click create button.
  15. Return to overview azure function and start container.
    Overview menu

Execution example

  1. Postman settings.
    Overview menu
  2. Select any DXF, DWG, DGN, DWF, DWFX, IFC, STL, DWT, IGES, PLT, CF2, OBJ, HPGL, IGS, PCL, FBX, PDF, SVG file.
  3. Click the send button.

More Examples

For more samples of how you can use Aspose.CAD in Docker, see the examples.

See Also