I'm trying to register a class as defined below with DI:
public class MyService
{
private string _serviceAddress;
public MyService(string serviceAddress)
{
_serviceAddress = serviceAddress;
}
public void DoWork()
{
// do work
}
}
In the program.cs, register it with DI:
builder.Services.AddSingleton<MyService>(new MyService(serviceAddress));
And in the razor component, inject it:
[Inject]
MyService myService { get; set; }
public WeatherForecast()
{
myService.DoWork(); // <- error points to this line
}
And when accessing the page, got NullReferenceException, pointing to the DoWork line above:
Unhandled exception rendering component: Exception has been thrown by the target of an invocation. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
Any advice is appreciated.