Error – System.MissingMemberException: The lazily-initialized type does not have a public, parameterless constructor
Error is self explanatory. The type do not have a public, parameterless constructor.
But how do I make Dependency injection in ASP.NET Core MVC to resolve to actual constructor I want.
The lazy type has a constructor with other dependencies those are registered in startup.cs. That is the constructor that I want.
I tried adding a default constructor. But I need to use the constructor with dependencies.
Application is an ASP.NET Core MVC App. I am not using Unity or any other DI frameworks, but using the one that comes with ASP.NET MVC Core.
I read in other posts that frameworks like Unity will resolve this automatically.
I want to know how this can be resolved in ASP.NET Core MVC.
I have a controller that depends on a type that is lazy initialized.
SampleController.cs
public class SampleController
{
private readonly Lazy<LazyTypeA> _LazyTypeA;
public SampleController(LazyTypeA lazyTypeA)
{
_LazyTypeA = lazyTypeA;
}
public Action TestAction()
{
_LazyTypeA.SomeMethod() <-- Errors at this line
}
}
LazyTypeA.cs
public class LazyTypeA
{
private readonly DependentClass _dependentClass ;
public LazyTypeA(DependentClass dependentClass )
{
_dependentClass = dependentClass ;
}
}
Startup.cs
services.AddTransient<DependentClass>();
services.AddTransient<Lazy<LazyTypeA>>();