How-to guide·Models.NET

Upload models concurrently from .NET

Upload a batch of models with one .NET client so the server checks them in at the same time, wait for all of them, read each result, and know what may run on which thread.

Checking in a model takes far longer than sending it: the server parses it, indexes it and generates its geometry. With the .NET client, Upload.UploadModel returns as soon as the file has been sent, with a task for the check-in. Starting all uploads first and waiting afterwards lets the server work on the whole batch at once.

Before you start#

The BIM.works_SDK package (.NET), an API token with the model:upload scope and write rights on the target folder, and the files. Upload a model explains a single upload, the deduplication check and what happens to a file with a name that already exists.

Steps#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using BIM.works_SDK;

class UploadConcurrently
{
    static async Task Main()
    {
        string[] files = Directory.GetFiles(@"path\to\models", "*.ifc");

        using (BimWorksClientFactory factory = new BimWorksClientFactory(new Uri("https://bim.works")))
        using (BimWorksClient client = factory.CreateClientWithApiToken("<your API token>"))
        {
            Guid rootUuid = client.Fs.GetRootNodeUuid();
            Guid folderUuid = client.Fs.CreateNodeIfNotExists(rootUuid, "Batch", true, InputFileType.FOLDER);

            // Start every upload from this thread. UploadModel returns as soon as the file is sent;
            // the server then checks in all of them at the same time
            var uploads = new List<(string File, AsyncUploadTask Task)>();
            foreach (string file in files)
            {
                try
                {
                    uploads.Add((file, client.Upload.UploadModel(new UploadBuilder(folderUuid).SetPath(file))));
                }
                catch (BimWorksException e)
                {
                    Console.WriteLine(Path.GetFileName(file) + " was not sent: " + e.Message);
                }
            }

            // Wait for all of them; a failed one does not stop the others
            try
            {
                await Task.WhenAll(uploads.Select(upload => upload.Task.GetTask()));
            }
            catch (Exception)
            {
                // Reported per file below
            }

            foreach (var (file, task) in uploads)
            {
                string name = Path.GetFileName(file);
                if (task.GetTask().IsFaulted)
                {
                    // The connection failed: there is no response to read
                    Console.WriteLine(name + " failed: " + task.GetTask().Exception.InnerException.Message);
                    continue;
                }
                try
                {
                    // Throws the server's error, a file the parser rejected for instance
                    TreeNodeVersion version = task.GetUploadResponse().treeNode.GetLastVersion();
                    Console.WriteLine(name + ": version " + version.Version + ", " + version.Status + ", " + version.Uuid);
                }
                catch (Exception e)
                {
                    Console.WriteLine(name + " failed: " + e.Message);
                }
            }
        }
    }
}

Uploads every .ifc file of a directory into a folder Batch under the token's root. Run it twice and every model gets a second version, because the names exist.

  1. Start every upload. client.Upload.UploadModel(builder) sends the file and returns an AsyncUploadTask for its check-in. A failure to send the file throws BimWorksException right there; everything the check-in finds wrong comes with its result.
  2. Wait for all of them. GetTask() is the System.Threading.Tasks.Task of a check-in, so Task.WhenAll (or Task.WaitAll) waits for the batch. One failed check-in does not stop the others.
  3. Read each result. A task whose Task is faulted lost its connection and has no response: check IsFaulted first, because Get() and GetUploadResponse() wait for a response that never comes. Otherwise GetUploadResponse() returns the model node with its new version, or throws the error the server reported for that file.

Threads#

  • Start the uploads from one thread. The client keeps a list of its running tasks that is not synchronised, and UploadModel adds to it.
  • Waiting for the tasks and reading their results can happen on any thread.
  • The files are sent one after another, each UploadModel call returning when its bytes are on the server; the check-ins run in parallel. To send the bytes of large files in parallel too, give each thread a client of its own: a factory can create several.
  • The synchronous calls (Fs, Bim.Query and the others) only share the client's HttpClient, which is thread-safe.

Check#

Every file prints its version number and the status OK, and the Batch folder lists them all (List the tree).