1

I am creating a library for our department to access multiple resources. Each resource type is in its own project inside the large solution. Each project will be built into its own NuGet package and needs to be completely independent.

All projects are running on .NET 8.

With .NET Native Injection, you register a context factory as follows (each library has its own set of contexts):

var builder = new WebApplicationOptions { Args = args, ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default } // Context with Read access builder.Services.AddDbContextFactory<AccessReadContext>(); (options => options .UseSqlServer(config.GetConnectionString("DatabaseContext")) .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)); // Context with Write access builder.Services.AddDbContextFactory<AccessWriteContext>(options => options .UseSqlServer(config.GetConnectionString("DatabaseContext"))); 

The business decided we should use LightInject for our DI container.

How do I register the context factories in the LightInject composition roots of each library without creating my own factory classes for each library?

I have tried multiple combinations of

public void Compose(IServiceRegistry serviceRegistry) { // this is a list of implementations I've tried serviceRegistry.Register(IDbContextFactory<AccessReadContext>>(); serviceRegistry.Register(typeof(IDbContextFactory<AccessReadContext>)); serviceRegistry.Register(typeof(IDbContextFactory<AccessReadContext>), typeof(AccessReadContext)); } 

And a whole lot of other combinations I could think of, but I always receive this error.

2025-05-07 18:00:21.1713|0|ERROR|Microsoft.AspNetCore.Diagnostics.DiagnosticsTelemetry.ReportUnhandledException|An unhandled exception has occurred while executing the request. System.InvalidOperationException: Unable to resolve type: CS.API.AccessLib.Repository.IAccessRepository, service name: ---> System.InvalidOperationException: Unresolved dependency [Target Type: CS.API.AccessLib.Repository.AccessRepository], [Parameter: writeContextFactory(Microsoft.EntityFrameworkCore.IDbContextFactory`1[CS.API.AccessLib.Data.MsSql.AccessWriteContext])], [Requested dependency: ServiceType:Microsoft.EntityFrameworkCore.IDbContextFactory`1[CS.API.AccessLib.Data.MsSql.AccessWriteContext], ServiceName:] at LightInject.ServiceContainer.GetEmitMethodForDependency(Dependency dependency) at LightInject.ServiceContainer.EmitConstructorDependency(IEmitter emitter, Dependency dependency) at LightInject.ServiceContainer.EmitConstructorDependencies(ConstructionInfo constructionInfo, IEmitter emitter, Action`1 decoratorTargetEmitter) at LightInject.ServiceContainer.EmitNewInstanceUsingImplementingType(IEmitter emitter, ConstructionInfo constructionInfo, Action`1 decoratorTargetEmitMethod) at LightInject.ServiceContainer.EmitNewInstance(ServiceRegistration serviceRegistration, IEmitter emitter) at LightInject.ServiceContainer.EmitNewInstanceWithDecorators(ServiceRegistration serviceRegistration, IEmitter emitter) at LightInject.ServiceContainer.<>c__DisplayClass219_0.<ResolveEmitMethod>b__0(IEmitter emitter) at LightInject.ServiceContainer.<>c__DisplayClass173_0.<CreateEmitMethodWrapper>b__0(IEmitter ms) at LightInject.ServiceContainer.CreateDynamicMethodDelegate(Action`1 serviceEmitter) at LightInject.ServiceContainer.CreateDelegate(Type serviceType, String serviceName, Boolean throwError) --- End of inner exception stack trace --- at LightInject.ServiceContainer.CreateDelegate(Type serviceType, String serviceName, Boolean throwError) at LightInject.ServiceContainer.CreateDefaultDelegate(Type serviceType, Boolean throwError) at LightInject.ServiceContainer.TryGetInstance(Type serviceType, Scope scope) at LightInject.Scope.TryGetInstance(Type serviceType) at LightInject.Microsoft.DependencyInjection.LightInjectServiceProvider.GetService(Type serviceType) at lambda_method9(Closure, IServiceProvider, Object[]) at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass6_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|7_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context) 

Using native injection works fine, however I have to move the injection into the libraries using LightInject to satisfy our requirement.

Any suggestion on how to approach this, or where to find additional documentation (I've read through the LightInject single page manual MANY times).

1
  • The list of implementations you've tried register AccessReadContext while the exception you receive is missing AccessWriteContext. Have you tried registering AccessWriteContext? What error do you get if you do? Commented May 17 at 17:48

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.