I have no extensive experience in testing, but I'm setting up a Singleton instance injection for a class I created. However, I don't know how to call that class. If I do call, I need constructors. If I create an additional empty constructor, the dependencies will appear as null.
I have spent a few days looking for it in the documentation, but it only shows examples of how to inject. I cannot find how to instantiate the class afterwards.
Also, I see some examples, but many of them are using MVC Controllers, which are instantiated automatically by the framework.
Connector.cs
public class Connector
{
private IConfiguration _configuration;
public Connector(IConfiguration configuration) {
_configuration = configuration;
}
public Connector() {
}
public string DoSomething() {
//return Something related to _configuration
}
}
startup.cs
public void ConfigureServices(IServiceCollection services)
{
//Other default config
services.AddSingleton(new Connector(Configuration));
}
HomeController.cs
public IActionResult Index()
{
var x = (new Connector()).DoSomething(); //This will fail as _configuration is not injected
return View();
}
How can I call Connector with the injected Configuration? Am I missing any dependency resolving? Am I calling the class incorrectly?
I hope somebody can shed some light on this.