Rafal
Rafal

Reputation: 33

Singleton in ASP.NET Core allows multiple instances

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.

Upvotes: 3

Views: 902

Answers (2)

marc_s
marc_s

Reputation: 755451

I believe the problem is how you've defined the Id property on your Person class:

public class Person : IPerson
{
    public string id => Guid.NewGuid().ToString();
}

This means: every time the code asks for the id - it gets a newly created Guid - so of course you keep getting new/different Guid's every time you look at your person!

You need to get the id once - in the Person constructor - and then return that:

public class Person : IPerson
{
    private string _id;

    public void Person()
    {
        _id = Guid.NewGuid().ToString();
    }

    public string id => _id;
}

I'm pretty sure once you've done this, the singleton will work - you keep getting back the same instance of Person every time you look at it.

Upvotes: 3

pyordanov11
pyordanov11

Reputation: 870

Person.id is a property defined with as expression-bodied property that returns a new Guid everytime it's called, so even though it might be the same instance, calling Person.id will get you a new value every time.

To properly test it define the class as:

public class Person : IPerson
{
  private string id;

  public Person()
  {
    this.id = Guid.NewGuid().ToString();
  }

  public string Id => this.id;
}

Now id field will be initialized upon class creation and the Id property should return the same value for each request.

Upvotes: 2

Related Questions