ServiceRegistry configures Lamar services inside an ASP.NET Core application. Pass it to UseLamar() to register application and framework services, then let Lamar build dependency trees when the application creates services. section 31.3
A practical setup follows these steps:
- Install Lamar.Microsoft.DependencyInjection and call UseLamar() on WebApplicationBuilder.Host. section 31.3
- Register framework services such as authorization and controllers through the ServiceRegistry. section 31.3
- Use default conventions or assembly scanning to register concrete classes and conventionally named services. section 31.3
Listing 31.16 Configuring Lamar as a third-party DI container
builder.Host.UseLamar(services => #1 { services.AddAuthorization(); #2 services.AddControllers() #2 .AddControllersAsServices(); #3 services.Scan(_ => { #4 _.AssemblyContainingType(typeof(Program)); #4 _.WithDefaultConventions(); #4 }); #4 } #1 Configures your services in UseLamar() instead of on builder.Services #2 You can (and should) add ASP.NET Core framework services to the ServiceRegistry, as usual. #3 Required so that Lamar is used to build your web API controllers #4 Lamar can automatically scan your assemblies for services to register.
Shows a complete Lamar configuration, including framework services, controller activation, and assembly scanning. section 31.3
