Skip to content

Instantly share code, notes, and snippets.

@SirRufo
Created March 26, 2023 09:35
Show Gist options
  • Save SirRufo/7233e84f6f39d2a0bb13442f10960f1a to your computer and use it in GitHub Desktop.
Save SirRufo/7233e84f6f39d2a0bb13442f10960f1a to your computer and use it in GitHub Desktop.
IServiceScopeFactory
public class FooService
{
private readonly IServiceScopeFactory _scopeFactory;
public FooService( IServiceScopeFactory scopeFactory )
{
_scopeFactory = scopeFactory;
}
public void DoSomething()
{
using IServiceScope scope = _scopeFactory.CreateScope();
var barService = scope.Services.GetRequiredService<IBarService>();
barService.DoWhateverWithTheService();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment