| Severity | Error |
| Reported by | Metalama.Extensions.DependencyInjection |
| Message | The dependency '<DependencyType>' in type '<TargetType>' cannot be handled because no dependency injection framework is registered. Register a framework by calling the RegisterFramework method from ConfigureDependencyInjection in a fabric. |
Cause
The DependencyAttribute aspect, the IntroduceDependencyAttribute advice, and the IntroduceDependency method don't generate the code that pulls a dependency themselves. They delegate this to a dependency injection framework, an implementation of IDependencyInjectionFramework registered for the project, namespace, or type.
By default, two frameworks are registered: LoggerDependencyInjectionFramework, which handles ILogger dependencies, and DefaultDependencyInjectionFramework, which handles other dependencies with the .NET Core pattern. A fabric can remove them with the UnregisterFramework method of the options builder passed to ConfigureDependencyInjection.
Metalama reports LAMA0701 when a dependency must be handled but the list of registered frameworks is empty. In the message, <DependencyType> is the type of the dependency and <TargetType> is the type into which the dependency must be introduced.
In the following example, the project fabric unregisters both built-in frameworks without registering another one, so the [Dependency] aspect on the _clock field has no framework to handle it.
1using Metalama.Extensions.DependencyInjection;
2using Metalama.Extensions.DependencyInjection.Implementation;
3using Metalama.Framework.Fabrics;
4
5namespace Doc.LAMA0701.Error;
6
7internal class Fabric : ProjectFabric
8{
9 public override void AmendProject( IProjectAmender amender )
10 {
11 amender.ConfigureDependencyInjection(
12 builder =>
13 {
14 // Both built-in frameworks are unregistered, and no other framework is registered.
15 builder.UnregisterFramework<LoggerDependencyInjectionFramework>();
16 builder.UnregisterFramework<DefaultDependencyInjectionFramework>();
17 } );
18 }
19}
20
1using Metalama.Extensions.DependencyInjection;
2using System;
3
4namespace Doc.LAMA0701.Error;
5
6public interface IClock
7{
8 DateTime Now { get; }
9}
10
11public partial class InvoiceService
12{
13 // Error: no dependency injection framework is registered to handle this dependency.
14 [Dependency]
Error LAMA0701: The dependency 'IClock' in type 'InvoiceService' cannot be handled because no dependency injection framework is registered. Register a framework by calling the RegisterFramework method from ConfigureDependencyInjection in a fabric.
15 private IClock _clock;
16
17 public DateTime GetDueDate() => this._clock.Now.AddDays( 30 );
18}
19
Resolution
Make sure that at least one framework is registered for the type:
- If you unregistered a built-in framework to replace it, register your own framework with the RegisterFramework method in the same ConfigureDependencyInjection call.
- Otherwise, don't unregister DefaultDependencyInjectionFramework.
In the following example, the fabric unregisters only the logger framework, and the default framework handles the dependency.
1using Metalama.Extensions.DependencyInjection;
2using Metalama.Extensions.DependencyInjection.Implementation;
3using Metalama.Framework.Fabrics;
4
5namespace Doc.LAMA0701.Fixed;
6
7internal class Fabric : ProjectFabric
8{
9 public override void AmendProject( IProjectAmender amender )
10 {
11 amender.ConfigureDependencyInjection(
12 builder =>
13 {
14 // Only the logger framework is unregistered. The default framework stays registered.
15 builder.UnregisterFramework<LoggerDependencyInjectionFramework>();
16 } );
17 }
18}
19
1using Metalama.Extensions.DependencyInjection;
2using System;
3
4namespace Doc.LAMA0701.Fixed;
5
6public interface IClock
7{
8 DateTime Now { get; }
9}
10
11public partial class InvoiceService
12{
13 // Fixed: the default framework is registered and handles this dependency.
14 [Dependency]
15 private IClock _clock;
16
17 public DateTime GetDueDate() => this._clock.Now.AddDays( 30 );
18}
19
1using Metalama.Extensions.DependencyInjection;
2using Metalama.Framework.RunTime;
3using System;
4
5namespace Doc.LAMA0701.Fixed;
6
7public interface IClock
8{
9 DateTime Now { get; }
10}
11
12public partial class InvoiceService
13{
14 // Fixed: the default framework is registered and handles this dependency.
15 [Dependency]
16 private IClock _clock;
17
18 public DateTime GetDueDate() => this._clock.Now.AddDays(30);
19
20 public InvoiceService([AspectGenerated] IClock? clock = null)
21 {
22 this._clock = clock ?? throw new System.ArgumentNullException(nameof(clock));
23 }
24}
25