Search code examples
c#dependency-injection.net-8.0autocadautodesk-designautomation

How to use dependency injection with latest AutoCAD API 2025 where .NET 8 was introduced?


New AutoCAD 2025 comes with .NET 8 which is basically .NET Core so I am assuming dependency injection should be a no brainer for autocad addins as well, but..

I haven't found a single word about it on whole internet, yet it should be a very basic way how to handle dependencies if they are going with .NET 8. Is there at least tiny example how to register services, configure some options and build service container for AutoCAD addon and consume it somewhere else like command method? Autodesk blogs are silent about this. I need to use my previously written services that I use for ASP.NET core WEB API projects for working with Forge APIs inside AutoCAD design automation plugin.

I have tried to use HostBuilder and register a single foo service but it won't even load the plugin, no error.


Solution

  • so I am assuming dependency injection should be a no brainer for autocad addins as well

    It is hard to know what you mean. It is definitely possible to use code like this in an addin:

    var services = new ServiceCollection();
    services.AddSingleton<IMyService, MyService>();
    var sp = services.BuildServiceProvider();
    sp.GetRequiredService<IMyService>();
    

    What are you looking for exactly?