[Civil3D]FileNotFoundException: System.Buffers, Version=4.0.2.0 when serializing with SpeckleObjectSerializer

Objective

I’m developing a Civil 3D .NET add-in that exports drawing entities (like polylines) into Speckle objects for real-time visualization and data sharing across platforms.
The goal is to serialize AutoCAD geometry (e.g., Polyline, Polyline3d) into Speckle JSON using the Speckle SDK 3.x and later send it to other applications or services.

Issue

When I call:

var serializer = new SpeckleObjectSerializer();
var json = serializer.Serialize(specklePolyline);

I get a FileNotFoundException inside the Civil 3D process (acad.exe):

FileNotFoundException: Could not load file or assembly
'System.Buffers, Version=4.0.2.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'

However:

  • I did not remove or relocate any NuGet packages from the output folder.
  • The same build works fine when run outside Civil 3D in a standalone .NET Framework test app.
  • The issue only occurs when loaded inside Autodesk’s runtime (AppDomain context of acad.exe).

I suspect it’s related to Civil 3D’s custom load context or dependency resolution behavior for assemblies, but I’m unsure how Speckle expects to resolve dependencies under .NET Framework hosts like AutoCAD.

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Speckle.Objects.Geometry;
using Speckle.Sdk.Common;
using Speckle.Sdk.Models;
using Speckle.Sdk.Serialisation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using Polyline = Speckle.Objects.Geometry.Polyline;

namespace KCMC_Civil.Commands.Test_Command
{
    internal static class SpeckleTypeInit
    {
        private static bool _done;
        public static void EnsureInitialized()
        {
            if (_done) return;
            try
            {
                var modelsAsm  = typeof(Speckle.Sdk.Models.Base).Assembly;
                var objectsAsm = typeof(Speckle.Objects.Geometry.Polyline).Assembly;

                var type = Type.GetType("Speckle.Sdk.Host.TypeLoader, Speckle.Sdk", throwOnError: false);
                var init = type?.GetMethod("Initialize",
                    BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                init?.Invoke(null, new object[] { new Assembly[] { modelsAsm, objectsAsm } });

                _done = true;
            }
            catch { /* ignore load context issues */ }
        }
    }

    public class SpeckleExportService
    {
        public void ExportSelectedPolylineToSpeckleJson()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var ed  = doc.Editor;
            var db  = doc.Database;

            SpeckleTypeInit.EnsureInitialized();

            try
            {
                var peo = new PromptEntityOptions("\nChoose Polyline to export to Speckle JSON");
                peo.AddAllowedClass(typeof(AcDb.Polyline),  false);
                peo.AddAllowedClass(typeof(AcDb.Polyline2d), false);
                peo.AddAllowedClass(typeof(AcDb.Polyline3d), false);
                var per = ed.GetEntity(peo);
                if (per.Status != PromptStatus.OK) return;

                Polyline specklePolyline;
                using (var tr = db.TransactionManager.StartTransaction())
                {
                    var ent = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Entity;
                    specklePolyline = ConvertToSpecklePolyline(tr, ent, Units.Meters);
                    tr.Commit();
                }

                var serializer = new SpeckleObjectSerializer();
                var json = serializer.Serialize(specklePolyline);

                var path = Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                    $"speckle_polyline_{DateTime.Now:yyyyMMdd_HHmmss}.json"
                );
                File.WriteAllText(path, json);

                ed.WriteMessage($"\nSpeckle JSON saved: {path}");
            }
            catch (Exception ex)
            {
                ed.WriteMessage($"\nSpeckle save failed: {ex.Message}");
            }
        }
    }
}

This topic was automatically closed after 180 days. New replies are no longer allowed.