| Severity | Error |
| Reported by | Metalama.Extensions.DependencyInjection |
| Message | None of the registered dependency injection frameworks can handle the dependency '<DependencyType>' in type '<TargetType>'. Note that the default framework does not support static dependencies. |
Cause
The DependencyAttribute aspect, the IntroduceDependencyAttribute advice, and the IntroduceDependency method delegate the code generation to the dependency injection frameworks registered for the project. Before choosing a framework, Metalama asks each registered framework whether it can handle the dependency by calling its CanHandleDependency method.
Metalama reports LAMA0702 when at least one framework is registered but all of them refuse the dependency. In the message, <DependencyType> is the type of the dependency and <TargetType> is the type into which the dependency must be introduced.
With the built-in frameworks, the usual cause is a static dependency: DefaultDependencyInjectionFramework pulls dependencies from the constructor, so it only accepts instance fields and properties, and LoggerDependencyInjectionFramework only accepts instance ILogger dependencies. A static ILogger field or property is therefore refused by both built-in frameworks and reported as LAMA0702. A dependency is static when the field or property marked with [Dependency] or [IntroduceDependency] is static. A custom framework can refuse a dependency for its own reasons.
In the following example, the [Dependency] aspect is applied to a static field.
1using Metalama.Extensions.DependencyInjection;
2using System;
3
4namespace Doc.LAMA0702.Error;
5
6public interface IClock
7{
8 DateTime Now { get; }
9}
10
11public partial class InvoiceService
12{
13 // Error: the default framework can't handle a static dependency.
14 [Dependency]
Error LAMA0702: None of the registered dependency injection frameworks can handle the dependency 'IClock' in type 'InvoiceService'. Note that the default framework does not support static dependencies.
15 private static IClock _clock;
16
17 public DateTime GetDueDate() => _clock.Now.AddDays( 30 );
18}
19
Resolution
Depending on the cause, do one of the following:
- Make the field or property non-static, as in the following example.
- If you use a custom framework, check the conditions of its CanHandleDependency method, or register a framework that supports this kind of dependency with the RegisterFramework method.
1using Metalama.Extensions.DependencyInjection;
2using System;
3
4namespace Doc.LAMA0702.Fixed;
5
6public interface IClock
7{
8 DateTime Now { get; }
9}
10
11public partial class InvoiceService
12{
13 // Fixed: the dependency is an instance field.
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.LAMA0702.Fixed;
6
7public interface IClock
8{
9 DateTime Now { get; }
10}
11
12public partial class InvoiceService
13{
14 // Fixed: the dependency is an instance field.
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