- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I've spent a lot of time on this, and I have tried searching here fruitlessly, so I apologize in advance if this has been asked and answered before, but I'm at the end of my knowledge.
We essentially want to build an application that will load a block (or drawing) with attributes and populate those attributes with data read from a database. Initially we had envisioned this as a standalone exe, but I quickly realized in process was likely what I wanted. I was able to figure out how to populate attributes in a block, so I then tried to add the database connection to the application. As we have a few other tools (non Acad) that utilize Entity Framework to access a database we wanted to maintain this functionality. Also, ORM's are clean and appealing. I simply could not get it to work, so I backed up a step and started with a simple helloworld autocad program as below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
//using Autodesk.AutoCAD.DatabaseServices;
//using Autodesk.AutoCAD.Geometry;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace wtf2
{
public class wtf
{
[CommandMethod("holaworld")]
public void HolaWorld()
{
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
ed.WriteMessage("Hola world! ... it works ... ok... great...");
}
}
}
At this stage I've simply added references to accoremgd.dll, acdbmgd.dll, acmgd.dll and everything works. But, EF is not installed yet so that's the next step and that is where things go wrong.
It took me a long time to figure out version compatibility with entity framework and autocad development, but I finally figured out that AutoCad depends on Framework and I cannot use .NET6 so I created a dll project using Framework 4.8.1. I then used NuGet to install entity framework. I am connecting to an existing database so I want to scaffold that so I install: Microsoft.EntityFrameworkCore.Tools (v3.1.31 for compatibility with Framework 4.8.1) and that installs a number of dependencies, including the requisit Microsoft.EntityFrameworkCore.Design and Microsoft.EntityFrameworkCore, both version 3.1.31. As this is going to connect to a MariaDB database I then use nuget to install MySql.EntityFrameworkCore (v3.1.27).
I then run the following scaffold command, edited for my particular use case, and it works. The EntityFramework code has been created and it is located where I want to use it.
Scaffold-DbContext "connection_string" "MySql.EntityFrameworkCore" -OutputDir <dir-name> -Tables <tables> -Context <context-name> -f
So, without making any additional changes or doing anything with the database code, I build the project (successfully) and then load the resulting dll into AutoCad 2023 with NETLOAD, but now I get error message within AutoCad
Cannot load assembly. Error details: System.IO.FileNotFoundException: Could not load file or assembly 'System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
at System.Reflection.RuntimeAssembly.GetExportedTypes()
at Autodesk.AutoCAD.ApplicationServices.AutoCADApplicationHolder.Initialize(Assembly assembly)
at Autodesk.AutoCAD.ApplicationServices.ExtensionLoader.ProcessAssembly(Assembly assembly)
=== Pre-bind state information ===
LOG: DisplayName = System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
(Fully-specified)
LOG: Appbase = file:///C:/Program Files/Autodesk/AutoCAD 2023/
LOG: Initial PrivatePath = NULL
Calling assembly : Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Program Files\Autodesk\AutoCAD 2023\acad.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
LOG: Attempting download of new URL file:///C:/Program Files/Autodesk/AutoCAD 2023/System.Threading.Tasks.Extensions.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/Autodesk/AutoCAD 2023/System.Threading.Tasks.Extensions/System.Threading.Tasks.Extensions.DLL.
LOG: Attempting download of new URL file:///C:/Program Files/Autodesk/AutoCAD 2023/System.Threading.Tasks.Extensions.EXE.
LOG: Attempting download of new URL file:///C:/Program Files/Autodesk/AutoCAD 2023/System.Threading.Tasks.Extensions/System.Threading.Tasks.Extensions.EXE.
LOG: Attempting download of new URL file:///Z:/Matalino Design/Projects/Duco Development/LoopDrawings/code/wtf2/wtf2/bin/Debug/System.Threading.Tasks.Extensions.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Revision Number
LOG: Attempting download of new URL file:///Z:/Matalino Design/Projects/Duco Development/LoopDrawings/code/wtf2/wtf2/bin/Debug/System.Threading.Tasks.Extensions/System.Threading.Tasks.Extensions.DLL.
LOG: Attempting download of new URL file:///Z:/Matalino Design/Projects/Duco Development/LoopDrawings/code/wtf2/wtf2/bin/Debug/System.Threading.Tasks.Extensions.EXE.
LOG: Attempting download of new URL file:///Z:/Matalino Design/Projects/Duco Development/LoopDrawings/code/wtf2/wtf2/bin/Debug/System.Threading.Tasks.Extensions/System.Threading.Tasks.Extensions.EXE.
I googled around a while on this and I am completely stuck.
The version of System.Threading.Tasks.Extensions that NuGet added to my project is 4.2.0.1. I've confirmed this by looking at that dll in my project references in Studio Community. Additionally nuget created app.config and packages.config files within my project. app.config contains the following relevant section
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" /></dependentAssembly>
and packages.config contains:
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net481" />
Finally, in my wtf2.csproj file I have the following:
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
And this seems consistent within my project and seems like what is supposed to be there based on what I've searched.
So, I'm at my wits end. Everything in my project is pointing at 4.2.0.1 dll, but AutoCad is giving an error referencing 4.2.0.0 dll, and I simply have no idea what this means or what I should do. I've tried to provide an exhausting level of detail and this should be fully recreatable (except for the scaffold).
Solved! Go to Solution.