Skip to content

Transient Service

Transient services are a type of service that is instantiated every time it is requested. This means that a new instance of the service is created each time it is injected. Transient services are useful for lightweight, stateless services that do not need to maintain any shared state or resources.

public class MyTransientService
{
public string SomeValue { get; set; } = "Value";
}
public class MyPlugin(IDependencyService services) : IPlugin
{
[Subscribe]
public void OnPluginLoading(PluginLoadingEvent @event)
{
// This event is fired when any plugin is being loaded
// Skip all other plugins load events except ours
if (@event.Plugin != this)
return;
dependencies.Register(services =>
{
// Transient services are instantiated every time they are requested
services.AddTransient<MyTransientService>();
});
}
}
public class MySingletonService(MyTransientService transientService)
{
public void DoSomething()
{
// Access your transient service instance
Console.WriteLine(transientService.SomeValue);
}
}
public class MySingletonService(IServiceProvider services)
{
public void DoSomething()
{
// Access your transient service instance
var transientService = services.GetRequiredService<MyTransientService>();
Console.WriteLine(transientService.SomeValue);
// Each time you call GetRequiredService, a new instance of MyTransientService is created
}
}