Reputation: 1835
We are developing a class library in .Net 2.0 Core. We use dependency injection straight out of the box. We have some services that we want to resolve from the container as "Scoped".
How can we start (and end) such Scope in code. It would be nice to be able to do this during some of our more elaborate test scenarios.
Note: we do not use "ASP.Net Core"
Upvotes: 4
Views: 1108
Reputation: 273244
using Microsoft.Extensions.DependencyInjection; // it's an extension method
using (var scope = serviceProvider.CreateScope())
{
// in your new Scope, use the scope's ServiceProvider
var service = scope.ServiceProvider.GetService<SomeService>();
}
Upvotes: 6