.NET Core — Dependency Injection

Karim Samir
simplifycoding
Published in
2 min readMay 19, 2024

In ASP.NET Core, DI is built-in and easier to use than in ASP.NET Framework.

The lifetime of a dependency describes how long an object created by DI will be used by the system.

In .NET Core, there are three primary lifetimes for dependencies: Singleton, Scoped, and Transient. Each of these lifetimes has a different purpose and impact on the behavior of the system.

Singleton

public interface ISingletonService
{
Guid getId();
}

public class TestService : ISingletonService
{
Guid id;
public TestService()
{
id = Guid.NewGuid();
}

public Guid getId()
{
return id;
}
}

// In Program.cs :
builder.Services.AddSingleton<ISingletonService, TestService>();

app.MapGet("/Test", (
[FromServices] ISingletonService transientService,
[FromServices] ISingletonService transientService2
) =>
{
List<string> result = new List<string>();

var testInstance= transientService.getId().ToString();
var testInstance2= transientService2.getId().ToString();
result.Add(testInstance);
result.Add(testInstance2);

return result;
});

//Result : Same Guid => Same Instance
//["044ee650-d188-479d-8204-84818625c2fc","044ee650-d188-479d-8204-84818625c2fc"]

//Result After Refreshing Browser : Same Guid => Same Instance
//["044ee650-d188-479d-8204-84818625c2fc","044ee650-d188-479d-8204-84818625c2fc"]

Transient

public interface ITransientService
{
Guid getId();
}

public class TestService : ITransientService
{
Guid id;
public TestService()
{
id = Guid.NewGuid();
}

public Guid getId()
{
return id;
}
}

// In Program.cs
builder.Services.AddTransient<ITransientService, TestService>();

app.MapGet("/Test", ([FromServices] ITransientService transientService, [FromServices] ITransientService transientService2) =>
{
List<string> result = new List<string>();

var testInstance = transientService.getId().ToString();
var testInstance2 = transientService2.getId().ToString();
result.Add(testInstance);
result.Add(testInstance2);

return result;
});

//Result : Differents Guid
//["d80d54a9-911b-49b4-b22f-7dc8a68bcf98","b05c6052-69a0-43c0-8372-f4468b271e70"]

//Result After Refreshing Browser : Differents Guid
//["cba2cacc-8db6-4268-9a8a-77df9e5f3b8a","48fe2e19-2161-425f-b278-7d4cb7752943"]

Scoped

public interface IScopedService
{
Guid getId();
}

public class TestService : IScopedService
{
Guid id;
public TestService()
{
id = Guid.NewGuid();
}

public Guid getId()
{
return id;
}
}

//In program.cs
builder.Services.AddScoped<IScopedService, TestService>();

app.MapGet("/Test", ([FromServices] IScopedService transientService, [FromServices] IScopedService transientService2) =>
{
List<string> result = new List<string>();

var testInstance = transientService.getId().ToString();
var testInstance2 = transientService2.getId().ToString();
result.Add(testInstance);
result.Add(testInstance2);

return result;
});

//Result : Same Guid => Same Instance
//["bc4956d6-55cb-40f9-98ec-ada57ddb25d1","bc4956d6-55cb-40f9-98ec-ada57ddb25d1"]

//Result After Refreshing Browser : Same Guid, but are Differents of the first one
//["3399be39-abf9-48bb-9566-c13e09ba360b","3399be39-abf9-48bb-9566-c13e09ba360b"]
  • Singleton: 1 Object / N HTTP Request,
  • Scoped: 1 Object / 1 HTTP Request,
  • Transient: 1 Object / 1 Instance.

--

--