I have the class as:
public class ServiceUtility
{
private IHostingEnvironment _environment;
public ServiceUtility(IHostingEnvironment environment)
{
_environment = environment;
}
public string GetFilePath(string fileName)
{
return _environment.WebRootFileProvider.GetFileInfo(fileName)?.PhysicalPath;
}
}
Using in Startup.cs
services.AddScoped<ServiceUtility>();
Using ServiceUtility in another class:
public class LevelUp
{
private readonly ServiceUtility _serviceUtility;
public LevelUp(ServiceUtility serviceUtility)
{
_serviceUtility = serviceUtility;
}
public LevelUp(string input)
{
//here the _serviceUtility is null
}
public int UserId { get; set; }
public int Level { get; set; }
}
From controller I am using as:
var object = userList.Select(x => new LevelUp(x.Description));
When I am using the _serviceUtility in Levelup(string input) constructor, the value is always null.
I have also added in Startup.cs class:
services.AddScoped<LevelUp>();
What I am doing wrong here, what is the proper way of dependency injection with multiple constructors?