Exception thrown could not load assembly in AutoCAD 2025 .NET 8

Exception thrown could not load assembly in AutoCAD 2025 .NET 8

nshupeFMPE3
Advocate Advocate
1,524 Views
12 Replies
Message 1 of 13

Exception thrown could not load assembly in AutoCAD 2025 .NET 8

nshupeFMPE3
Advocate
Advocate

I've added ClosedXML to my plugin for AutoCAD 2025 using .NET 8.0. I can still compile and run my plugin. But when I launch a command that uses a method from a class that requires ClosedXML, I get an exception while debugging

System.IO.FileLoadException
  HResult=0x80131621
  Message=Could not load file or assembly 'ClosedXML, Version=0.104.1.0, Culture=neutral, PublicKeyToken=null'. Could not find or load a specific file. (0x80131621)


I've already verified that my .dll and the .dll of ClosedXML and it's dependencies are all present in the same folder.

I'm wondering if its a problem that ClosedXML is targeting .NET Standard? 

I've also tried to do Assembly.LoadFrom("ClosedXML.dll") in the command before calling the method that requires ClosedXML, but had the same exception occur. 

Here is the command and the code it's calling

[CommandMethod("MyTools", nameof(Test), CommandFlags.Modal | CommandFlags.UsePickSet)]
public void Test()
{
    Editor ed = _doc.Editor;
    Database db = _doc.Database;
    
    using Transaction tr = _doc.Database.TransactionManager.StartTransaction();  

    
    CSVExporter.Export(new Project.Project(_doc, tr));

    tr.Commit();

    
}
public class Project(Document doc, Transaction tr)
{
    public Document Document { get; } = doc;
    public Transaction Transaction { get; } = tr;
}
public class CSVExporter
{
    public static void Export(Project.Project project)
    {
        if (!project.Document.CreateDirectory("data", out var path))
        {
            project.Document.Editor.WriteMessage($"\nProblem finding or creating data folder.");
            return;
        }
        
        string fullFileName = Path.GetFileName(project.Document.Name);
        string fileName = Regex.Replace(fullFileName, @".dwg$", ".xlsx", RegexOptions.IgnoreCase);

        string newPath = Path.Combine(path, fileName);

        using var workbook = new XLWorkbook();
        var mainWorksheet = workbook.AddWorksheet("WCS");




        workbook.SaveAs(newPath);
    }
}



0 Likes
Accepted solutions (1)
1,525 Views
12 Replies
Replies (12)
Message 2 of 13

ActivistInvestor
Mentor
Mentor

You should verify that the assembly is targeting.net 8.0 because otherwise you will get this error when the runtime tries to load it. 

0 Likes
Message 3 of 13

nshupeFMPE3
Advocate
Advocate
My assembly is indeed targeting .NET 8, but from what I see on the Nuget description for ClosedXML it can target either .NET Standard 2.0 or 2.1.

It was my understanding that a .NET 8 environment could run a .NET Standard assembly, but I guess I'm partially mistaken?
0 Likes
Message 4 of 13

ActivistInvestor
Mentor
Mentor

I was referring to the ClosedXML Assembly, not your assembly. You will probably need to check out the repository and build a net 8.0 version of it. 

0 Likes
Message 5 of 13

ActivistInvestor
Mentor
Mentor

If it uses System.Data.SqlClient, check to see what version of that it is using. I'm pretty sure AutoCAD uses that assembly and if it does, then you must use the same version AutoCAD uses. 

Message 6 of 13

nshupeFMPE3
Advocate
Advocate

Thank you for the help, these are both good idea's I will try. By chance how would you determine the version of something like System.Data.SqlClient that AutoCAD uses? Though I will not that in this particular case I dont see that as a dependency for ClosedXML.

0 Likes
Message 7 of 13

ActivistInvestor
Mentor
Mentor

@nshupeFMPE3 wrote:

Thank you for the help, these are both good idea's I will try. By chance how would you determine the version of something like System.Data.SqlClient that AutoCAD uses? Though I will not that in this particular case I dont see that as a dependency for ClosedXML.


You can use Process Explorer or you can run this PowerShell command (as an admin):

 

Get-Process -Name acad | ForEach-Object { $_.Modules }

 

My advice would be to check-out the entire ClosedXML repository, open the project and upgrade it to .NET 8.0, since that's really the only way to identify one of possibly-many versioning issues.

0 Likes
Message 8 of 13

nshupeFMPE3
Advocate
Advocate

EDIT: Nevermind, stopped working again. Thats really weird...

Well I'm not sure why things started working, which is always kind of a weird feeling. 

I set the Environment variable CoreHost_Trace and CoreHost_TraceFile in the launch settings for debugging from Visual Studio and once I did that it started working? Even after I've removed the Environment Variables/ reset them its still working. 

Not sure if I should mark this as a solution (haha), but maybe restarting my Visual Studio or turning the computer off and on again would have worked just as well.

0 Likes
Message 9 of 13

fieldguy
Advisor
Advisor

FWIW

I just finished an update to .NET 8 that creates an XLSX workbook but it uses the DocumentFormat.OpenXml library.

I have used this library for a few years now.  It is provided with an acad 25 install

C:\Program Files\Autodesk\AutoCAD 2025\DocumentFormat.OpenXml.dll.

Copy local needs to be "Yes".

 

0 Likes
Message 10 of 13

nshupeFMPE3
Advocate
Advocate
Thank you for the suggestion @fieldguy. My plugin previous to 2025 used OpenXml and it does get the job done. But it's kind of clunky and annoying to use IMO. I'm at the point were ClosedXML is working for my plugin, just not when debugging mode is running, which is annoying but not the end of the world.
0 Likes
Message 11 of 13

dpaytonBZ25K
Explorer
Explorer

The issue is that AutoCAD 2025 and 2026 are using ClosedXML v0.100.3.0 and v0.102.2 respectively.

 

Use the ClosedXML NuGet that matches the version AutoCAD is using and you will be able to debug. 

 

Because AutoCAD is loading ClosedXML from the AutoCAD executing location, your plugin is going to use the version of ClosedXML AutoCAD is loading regardless of which NuGet package you have. Depending on what your plugin does, that may not matter. Where it could matter is that ClosedXML v0.100.0 through 0.102.3 have an issue that throws exceptions when trying to parse excel formulas.

Message 12 of 13

ActivistInvestor
Mentor
Mentor
Accepted solution

If AutoCAD is using ClosedXML, You can reference that assembly in rhe AutoCAD program folder. 

0 Likes
Message 13 of 13

dba78
Advocate
Advocate

You just saved me from going crazy 😁

I suffered on such exceptions in other contexts as well. Never came to the idea, ADSK would use them as well and to check the versions....

 

0 Likes