femto-backend/Femto.Common/ScopeBinding.cs

26 lines
690 B
C#

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Femto.Common;
/// <summary>
/// We use this to bind a scope to the request scope in the composition root
/// Any scoped services provided by this subcontainer should be accessed via a ScopeBinding injected in the host
/// </summary>
/// <param name="scope"></param>
public class ScopeBinding(IServiceScope scope) : IDisposable
{
private IServiceScope Scope { get; } = scope;
public T GetService<T>()
where T : notnull
{
return this.Scope.ServiceProvider.GetRequiredService<T>();
}
public virtual void Dispose()
{
this.Scope.Dispose();
}
}