Example Setup
I am using a MinimalApi
application. Create a new one if you need.
dotnet new webapi -n DITest
Open it in a IDE or editor of your choice.
public interface IMyService
{
Guid InstanceId { get; }
}
public class MyService : IMyService
{
public Guid InstanceId { get; }
public MyService()
{
InstanceId = Guid.NewGuid();
// We are logging in the constructor, so that we get notified whenever the instance is created
Console.WriteLine($"==> Service created with InstanceId: {InstanceId}");
}
}
IMyService
have a read-only property named InstanceId
of type Guid
, which is set from the constructor.
We are logging inside the constructor, so that we can get notified whenever the new instance is created.