Yes – just add the “bridge” package that Jeremy Miller (the author of StructureMap) published for ASP.NET Core / Microsoft.Extensions.DependencyInjection:
• NuGet package name: StructureMap.Microsoft.DependencyInjection
(for new projects you can use its successor Lamar.Microsoft.DependencyInjection, but the usage is identical).
What the package gives you
An extension method Populate() that copies everything you put in an IServiceCollection into a StructureMap Container.
Convenience extension methods UseStructureMap() (or UseLamar()) for IHostBuilder / IWebHostBuilder, so you don’t even have to call Populate manually.
Quick example (console / generic-host)
csharp
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using StructureMap; // or Lamar;
using StructureMap.Microsoft.DependencyInjection;
Host.CreateDefaultBuilder(args)
.UseStructureMap() // swaps default provider with SM
.ConfigureServices(services =>
{
// “Plain” MS.DI syntax
services.AddTransient<IFoo, Foo>();
services.AddSingleton<IBar, Bar>();
})
.Build()
.Run();
Behind the scenes UseStructureMap() does roughly this:
csharp
var container = new Container();
container.Populate(services); // copies ServiceDescriptors -> SM
return container.GetInstance<IServiceProvider>();
Minimal manual (no host builder)
csharp
var services = new ServiceCollection();
services.AddTransient<IFoo, Foo>();
var container = new Container();
container.Populate(services);
// Resolve with StructureMap
var foo = container.GetInstance<IFoo>();
Package to install
powershell
dotnet add package StructureMap.Microsoft.DependencyInjection
# or the newer, actively-maintained successor
dotnet add package Lamar.Microsoft.DependencyInjection
A note on future-proofing 🚀
StructureMap is in maintenance-only mode; its author recommends switching to Lamar, which is API-compatible and noticeably faster. If you use the Lamar package your UseStructureMap() call simply becomes UseLamar() (and of course Container becomes Lamar.Container).
Either way, the answer to your question is “yes”: install the adapter package, and you can keep writing services.AddSingleton, AddTransient, etc. while StructureMap/Lamar runs the show at runtime.