I have implemented very simple code to check functionality of dependency injection in ASP.NET Core 2.1.
Unfortunately, I got others results as I expected. Every time, when I refresh site, I got another id instead the same.
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton<IPerson, Person>();
}
HomeController.cs
public class HomeController : Controller
{
private IPerson _person;
public HomeController( IPerson person)
{
_person = person;
}
public ViewResult Index()
{
return View( "Index", $"{_person.id}");
}
}
Person.cs
public interface IPerson
{
string id { get; }
}
public class Person : IPerson
{
public string id => Guid.NewGuid().ToString();
}
I suspect, the reason is very simply, but really don't understand what I made wrong.