-
Have a service in my ASP.Net core app that depends on another service and I'd like to expose them via dependency injection. What I ended up settling on is the following: services.AddScoped<Service1>(() =>
var service1 = ...;
// configure
return service1;
});
services.AddScoped<Service2>(serviceProvider =>
var service2 = ...
service2.Service1 = (Service1)serviceProvider.GetService(typeof(Service1));
return service2;
}); That feels a bit wrong though because it's not declarative and pretty much all of the DI samples for ASP.NET have been declarative. I feel like I should be looking for an overload if Is this the correct approach for this pattern? Note: I realize I could make this declarative by defining a third type say |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Why aren't you using constructors? |
Beta Was this translation helpful? Give feedback.
Why aren't you using constructors?